Recent

Author Topic: Is this an error involving with clause ?  (Read 3746 times)

abarchman

  • New Member
  • *
  • Posts: 11
Is this an error involving with clause ?
« on: February 27, 2017, 06:11:34 am »
I found this piece of code online somewhere, and wondering if this is a compiler error. Please note that the second parameter is named "ItemIndex".  And also that within the with block below, the ListView has a ItemIndex property available to it.  It seems as if the "ItemIndex" that is being referenced is the ListView one and not the parameter.  Is the correct behavior?  The workaround was easy, just change the parameter name.

Am I correct in my my question?  The Editor's popups show it connected to the ListView.  Anyway the execution has an exception.




procedure TForm1.ListViewSetTopItem(ListView: TListView; ItemIndex: Integer);
var
  Difference, ItemHeight: Integer;
begin
  if not (ListView.ViewStyle = vsReport) then
    Exit;
  if (ItemIndex < 0) or (ListView.Items.Count = 0) or
      (ItemIndex > ListView.Items.Count - 1) then
    raise EInvalidOperation.CreateFmt(SPropertyOutOfRange, ['TopItem']);
  with ListView do
  begin
    Difference := TopItem.Index - Items.Item[ItemIndex].Index;   // <-- THE ONE IN THIS LINE
    with Items.Item[0].DisplayRect(drBounds) do
      ItemHeight := Top - Bottom;
    ScrollBy(0, Difference *  ItemHeight);
  end;
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is this an error involving with clause ?
« Reply #1 on: February 27, 2017, 08:16:00 am »
I found this piece of code online somewhere, and wondering if this is a compiler error. Please note that the second parameter is named "ItemIndex".  And also that within the with block below, the ListView has a ItemIndex property available to it.  It seems as if the "ItemIndex" that is being referenced is the ListView one and not the parameter.  Is the correct behavior?  The workaround was easy, just change the parameter name.
with overrides top level scope for identifier resolution using the object being with-ed, so yes the behavior is correct. That's why by default generated codes use A prefix for parameter names. Some people even also use prefixes for local variables (L or V).

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Is this an error involving with clause ?
« Reply #2 on: February 27, 2017, 09:50:05 am »
In this case it's better to create an object for the item.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: Is this an error involving with clause ?
« Reply #3 on: February 27, 2017, 11:18:43 am »
No, it's better to NOT use with...
This is almost an exemplary example why not.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

abarchman

  • New Member
  • *
  • Posts: 11
Re: Is this an error involving with clause ?
« Reply #4 on: February 28, 2017, 06:25:54 am »
I agree that the with clause is inappropriate at times.  I just wish the syntax were similar to VB in that each use had a "." at the beginning.

Ex.

  with ListView1 do
  begin
    If .ItemIndex = ItemIndex then  // no confusion here
    ...

I can only wish.

 

TinyPortal © 2005-2018