[fpc-pascal]TCollection question

James Mills prologic at daisy.ods.org
Mon Jul 7 04:44:06 CEST 2003


On Sun, Jul 06, 2003 at 10:38:28PM +0100, Matt and Lisa Emson wrote:
> 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!!

Thank you for this. I was a tad confused as my original idea was wrong
in that I tried to create a pure descandent of TList and extend it...

I will use your basic design below, thank you :)

cheers
James

> 
> 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
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

-- 
-
- James Mills
Zero Defect Software Engineers Group - ZDSEG




More information about the fpc-pascal mailing list