[fpc-pascal]TCollection question

Matt and Lisa Emson memsom at interalpha.co.uk
Sun Jul 6 23:38:28 CEST 2003


Why not use a TObjectList?? IIRC that is available. In unit Contnrs.pas. It
will do a lot more for you. It can 'own' the objects, and therefore free you
from needing to manage them.

Simple descendent (typed at speed, with no testing):

N.B. This is a WRAPPER because it allows the programmer more control over
the interface to the class. However a descendent would work the same way
pretty much. You'd probably just alter the 'Add' to 'AddXXX' so as to not
clash with 'Add' from TList. Otherwise you would hide the method and make in
unavailable. This wrapper is really usefull... I use this basic design all
the time!!

TMyListItemClass = class; {your class to store}

TMyList = class
private
  FList: TList;
protected
  procedure SetItem( Index: integer; Value: TMyListItemClass);
  function GetItem(Index: integer): TMyListItemClass;
public
  Constructor Create; virtual;
  Destructor Destroy; override;

  function Add(AItem: TMyListItemClass): integer;
  procedure Delete( AIndex: integer );
  function Count: integer;

  property Items[Index: integer]: TMyListItemClass read GetItem write
SetItem;
end;

 procedure SetItem( Index: integer; Value: TMyListItemClass);
 begin
  FList[Index] := Value;
 end;

  function GetItem(Index: integer): TMyListItemClass;
  begin
    Result := TMyListItemClass(FList[Index] );
  end;

  Constructor Create; virtual;
  begin
    FList := TList.Create;
  end;

  Destructor Destroy; override;
  begin
     {don't forget some code to empty list....!!!!}
     FList.Free;

    inherited;
  end;

  function Add(AItem: TMyListItemClass): integer;
  begin
    result := FList.Add(AItem);
end;

procedure Delete( AIndex: integer );
begin
  FList.Delete(AIndex);
end;

  function Count: integer;
  begin
     Result := FList.Count;
  end;


----- Original Message -----
From: "James Mills" <prologic at daisy.ods.org>
To: <fpc-pascal at lists.freepascal.org>
Sent: Sunday, July 06, 2003 4:56 PM
Subject: Re: [fpc-pascal]TCollection question


> Actually Michael,
>
> Could you possibly spare 5 mins and give a really simple example of a
> TList descandent ? I'm a tad confused here, (too slowly getting
> anywhere)...
>
> Thank you :)
>
> cheers
> James
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list