[lazarus] More of a FPC question but... dynamic arrays

Andrew Johnson aj_genius at hotmail.com
Fri Oct 4 10:25:20 EDT 2002


On 04 Oct 2002 08:48:07 -0500
"Tony Maro" <tonym at nlisc.com> wrote:

>Type TMyObj = class(TObject)
>Private
>   FMyArray: ^TMyRecord
>public
>   procedure Add(MyItem: TMyRecord);
>   procedure Delete(MyIndex: Integer);
>   function Count: Integer;
>   function Items{here's where I lose it - is it Items(Index: Integer)?}
>end;

>{Sample usage:}
>MyObj := TMyObj.Create;

>MyObj.Add(ThisItem); // adds an item to the array
>MyItem := MyVar.Items[1]; // grabs the first item on array


the proper way would be to use a Property :

Type TMyObj = class(TObject)
Private
   FMyArray: ^TMyRecord
protected
  Function GetItem(Index : Integer) : TMyRecord;
public
   procedure Add(MyItem: TMyRecord);
   procedure Delete(MyIndex: Integer);
   function Count: Integer;
   property Items[Index: Integer] : TMyRecord read GetItem; Default;
end;

and you would put the actual routine in GetItem. The default allows you to 
read like myobj[0], aka you don't have to do a myobj.items[0]

Andrew

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com






More information about the Lazarus mailing list