[fpc-pascal] Arrays of objects

Matt Emson memsom at interalpha.co.uk
Wed Oct 31 10:39:23 CET 2007


Adrian Maier wrote:
> Hello,
>
> I'm seeking for advice about which is the best way to hold an array of
> class instances
> I need to access the elements using its position (like a regular
> array) , and also i'd like
> the structure to grow when I add more elements
TObjectList

If you want specific non type casted code, write a wrapper that exposes 
the obvious methods and properties:

type
   TMyObject = class
    public
        a: integer;
    end;


    TMyObjectListWrapper = class
    private
       flist: TObjectList;
       procedure SetItem(index: counter; item: TMyObject);
       function GetItem(index: integer): TMyObject;
     public
        function Count: integer;
        function Add(item: TObject): integer;
        procedure Delete(index: integer);
        procedure Clear;
        property Items[index: integer]:TMyObject read GetItem write SetItem;
    end;

untested because I have no Pascal to compile with, but you get the jist.

M
       



More information about the fpc-pascal mailing list