[DUG] Is it a bug in latest version of Delphi?

Jolyon Smith jsmith at deltics.co.nz
Tue Feb 16 11:55:30 NZDT 2010


... or a helper function that can be used on any TListBox without requiring
a modification introduced on a specific base class (easily adapted for other
control types, obviously)

uses
  RTLConsts;

type
  TListBoxHelper = class(TListBox);
  TStringsHelper = class(TStrings);


function ListBoxGetObject(const aListBox: TListBox; const aIndex: Integer):
TObject;
begin
  if (aIndex < 0) or (aIndex >= aListBox.Items.Count) then
    TStringsHelper(aListBox.Items).Error(SListIndexError, aIndex);

  result := TObject(TListBoxHelper(aListBox).GetItemData(aIndex));
end;


In this approach I separate the two entirely separate problems of an invalid
list index and an error in retrieving item data by specifically testing the
index for validity *before* attempting to retrieve the object.  Whatever
errors might conceivably occur as a result of calling GetItemData() you at
least won't get a spurious and misleading "list index out of bounds".



> -----Original Message-----
> From: delphi-bounces at delphi.org.nz [mailto:delphi-
> bounces at delphi.org.nz] On Behalf Of Karl Reynolds
> Sent: Tuesday, 16 February 2010 11:16 a.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: Re: [DUG] Is it a bug in latest version of Delphi?
> 
> One way to get around this would be to add your own descendant of
> TListBox and add an Objects property that works as you would expect
> (ie. use that instead of items.objects). Here's a quick sample I
> knocked together:
> 
> >>
> unit MyListBox;
> 
> interface
> 
> uses
>   SysUtils, Classes, Controls, StdCtrls, RTLConsts;
> 
> type
>   TMyListBox = class(TListBox)
>   private
>     function GetObject(AIndex: Integer): TObject;
>     procedure SetObject(AIndex: Integer; const AValue: TObject);
>     { Private declarations }
>   protected
>     { Protected declarations }
>   public
>     property Objects[AIndex: Integer]: TObject read GetObject write
> SetObject;
>   published
>     { Published declarations }
>   end;
> 
> procedure Register;
> 
> implementation
> 
> type
>   TIPRStrings = class(TStrings);
> 
> function TMyListBox.GetObject(AIndex: Integer): TObject;
> begin
>   Result := TObject(GetItemData(AIndex));
>   if GetLastError <> 0 then
>     TIPRStrings(Items).Error(SListIndexError, AIndex);
> end;
> 
> procedure TMyListBox.SetObject(AIndex: Integer; const AValue: TObject);
> begin
>   Items.Objects[AIndex] := AValue;
> end;
> 
> procedure Register;
> begin
>   RegisterComponents('Samples', [TMyListBox]);
> end;
> 
> end.
> <<
> 
> Alternatively, you could just use the relevant code yourself on your
> own listbox where you need to
> 
> type
>   TIPRCustomListBox = class(TListBox);
> 
> procedure TForm1.Button1Click(Sender: TObject);
> var
>   i: Integer;
> begin
>   ListBox1.Items.AddObject('foo', TObject(-1));
>   i := Integer(TIPRCustomListBox(ListBox1).GetItemData(0));
>   if GetLastError <> 0 then // an error occurred, raise an exception or
> whatever
>   else ShowMessage(IntToStr(i));
> end;
> 
> Cheers,
> Karl
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with
> Subject: unsubscribe



More information about the Delphi mailing list