[fpc-pascal]TCollection question

Marco van de Voort marcov at stack.nl
Sun Jul 6 17:31:59 CEST 2003


> On Sun, Jul 06, 2003 at 05:11:17PM +0200, Marco van de Voort wrote:
> > > which hold a dynamic array of TNick ... Is this what TList already does
> > > (if so I'm wasting my time writing my own code...) ?
> > 
> > Pretty much yes, but in a class wrapper. You can add and remove items,
> > iterate through them etc.  Maybe you can derive a class from TList to
> > customize it a bit.
> 
> However TList by itself would work though ?

Basically yes. But derive it anyway, in case you want to extend it later.
 
> > - you yourself have to make sure that elements are properly freed, so
> >    when deleting object A, get a reference to it, delete it from the list, 
> >    and then free the object. (however that can be automized in the class
> >    warpper)
> Is this all done and automated in TList ?

No. So you have to that yourself. This because TList can also store pointers
to records, which don't have to be finalised. So deallocation is still
manual.

But it is trivial. Assume you want to delete entry 3, assuming that variable
TheList is the TList:

var t : MyObjectType

t:=MyObjectType(TheList[3]);
TheList.delete(3);
t.free;

You can solve it like this: (untested)

type tmyTList = class TList
		  public
	            procedure DeleteCompletely(index :Integer);
		  end;

procedure tmyTList.DeleteCompletely(index: Integer);

var t : MyObjecType;

begin
  t:=MyObjectType(Items[3]);
  delete(3);
  if t<>NIL
   t.free;
end; 

See TList as a versatile baseclass to quickly build a dedicated, very
easy to use container on top of.

> > There is FCL documentation somewhere (and it will be in the next full 
> > release), but I couldn't find a recent version so fast.
> > 
> > So I put down a very old version (April 2002) on the web here:
> > 
> > www.stack.nl/~marcov/fcl.pdf
> 
> I think it's here also in html format:
> http://www.nl.freepascal.org/docs-html/fcl/classes/tlist.html

That's the place I meant yes.




More information about the fpc-pascal mailing list