Who cares where "-1" points to? - if you don't *use* TObject(-1) *as* a TObject 
then it's simply smoke and mirrors to get the compiler to allow you to store a 
particular pattern of 32-bits in a storage slot that is "typed" in a particular 
way.


But yes, this code causes an error in BDS 2006, thanks to this bit of nonsense 
in the VCL:

function TListBoxStrings.GetObject(Index: Integer): TObject;
begin
  if ListBox.Style in [lbVirtual, lbVirtualOwnerDraw] then
    Result := ListBox.DoGetDataObject(Index)
  else
  begin
    Result := TObject(ListBox.GetItemData(Index));
**    if Longint(Result) = LB_ERR then Error(SListIndexError, Index); **
  end;
end;


The line with ** ** should simply be testing "Index" imho, and doing so 
*before* attempting to retrieve the item data and casting as TObject.

However, the same code does NOT cause an error with a combo-box!

The equivalent code in the VCL for a combo (TCustomComboBoxStrings.GetObject) 
contains this pertinent comment and modification to the error checking:

  // Do additional checking on Count and Index here is so in the event
  // the object being retrieved is the integer -1 the call will succeed
  if (Longint(Result) = CB_ERR) and ((Count = 0) or (Index < 0) or (Index > 
Count)) then
    Error(SListIndexError, Index);


But again, I believe a simple check on "Index" before attempting to retrieve 
the item data would still be more correct an entirely separate any concerns of 
the "value" of the data and the expected returned result.


> -----Original Message-----
> From: delphi-boun...@delphi.org.nz [mailto:delphi-
> boun...@delphi.org.nz] On Behalf Of Alister Christie
> Sent: Tuesday, 16 February 2010 10:20 a.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: Re: [DUG] Is it a bug in latest version of Delphi?
> 
> I can confirm that
>   ListBox1.Items.AddObject('foo', TObject(-1));
>   ShowMessage(IntToStr(Integer(ListBox1.Items.Objects[0])));
> gives an error but
>   ListBox1.Items.AddObject('foo', TObject(0));
>   ShowMessage(IntToStr(Integer(ListBox1.Items.Objects[0])));
> does not
> 
> I think casting -1 to an object is probably not the best idea - what
> memory location does this point to anyway? 0xFFFFFFFF?
> 
> Alister Christie
> Computers for People
> Ph: 04 471 1849 Fax: 04 471 1266
> http://www.salespartner.co.nz
> PO Box 13085
> Johnsonville
> Wellington
> 
> 
> 
> sinu sudhakaran wrote:
> > Hi all,
> >
> > I came across a strange bug(???) in latest versions of Delphi.
> >
> > Have a look at the following code.
> >
> > procedure TForm1.Button1Click(Sender: TObject);
> > var
> > i : integer;
> > begin
> > ComboBox1.Clear;
> > ComboBox1.Items.AddObject('All Locations', TObject(-1));
> > ComboBox1.Items.AddObject('Only this Location', TObject(0));
> > ComboBox1.Items.AddObject(Test Location', TObject(1));
> >
> > i := Integer(ComboBox1.Items.Objects[0]);
> > showmessage(inttostr(i));
> > end;
> >
> > When I tried to run this code in Delphi 6 and Delphi 2007 , I am
> > getting List Index out of bounds[0] error in Showmessage. When I
> tried
> > -2, -3, -4… instead of -1 object , I got the proper output without
> any
> > List index out of bounds error. Also I am getting -1 without any
> error
> > when I tried this code in Delphi 3.
> >
> > Is it a bug in latest version of Delphi ? Any help is appreciated.
> >
> > Thank you
> >
> > Regards,
> > Sinu
> >
> >
> <http://sigads.rediff.com/RealMedia/ads/click_nx.ads/www.rediffmail.com
> /signatureline....@middle?>
> >
> >
> > ---------------------------------------------------------------------
> ---
> >
> > _______________________________________________
> > NZ Borland Developers Group - Delphi mailing list
> > Post: delphi@delphi.org.nz
> > Admin: http://delphi.org.nz/mailman/listinfo/delphi
> > Unsubscribe: send an email to delphi-requ...@delphi.org.nz with
> Subject: unsubscribe
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi@delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-requ...@delphi.org.nz with
> Subject: unsubscribe


_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe

Reply via email to