In article <01bc7209$fcb62980$4e89b...@mikey.interalpha.co.uk>,
"Mikey" <micha...@interalpha.co.uk> wrote:
Quote
>Hello again,
> I am using delphi 2.0 and have found that when creating
>types, filling them with information, and chucking them into a tlist
>works quite well until you try to destroy or free them ? I know that a
>tlist only holds a pointer to the type that I have created and have tryed
>to pass this to the destroy and dispose methods but to no avail
TLists are used quite a lot merely to refer to a whole lot of other
things. One of my personal favorite uses is, when making a new class
based on TComponent (or any of its descendants), to override the
Notification method...
Let's say I wanted to keep track of all my TThingy objects (TThingy also
being based on TComponent)...
Add this to the private section of TMyComponent:
FThingies : TList;
function GetThingy(AIndex: Integer): TThingy;
Add this to the protected section:
procedure Notification(AComponent: TComponent;AOperation: TOperation);
override;
Add this to the public section
property Thingy[AIndex: Integer]: TThingy read GetThingy;
For your get, add:
function TMyComponent.GetThingy(AIndex: Integer);
begin
Result := TThingy(FThingies[AIndex]);
end;
In your constructor, add:
FThingies := TList.Create;
In your destructor, add:
FThingies.Free;
In your Notification method:
procedure TMyComponent.Notification(AComponent: TComponent;AOperation:
TOperation);
begin
if AComponent is TThingy then
if Operation = opInsert then
FThingies.Add(AComponent)
else
FThingies.Remove(AComponent);
end;
Now, if you create a TThingy like this:
MyThingy := TThingy.Create(MyComponentObject);
It automatically adds it to the thingy list in MyComponentObject.
You can then access MyComponentObject.Thingy[0].
Similarly, MyThingy.Free will remove it.
Quote
>any one got any ideas ?
If you're trying to work with the Free and/or Dispose with the items
in a TList, remember that a generic Pointer won't give you much info.
If you've got a class in your TList, you can free it thusly:
TMyClass(MyList[X]).Free;
If you've got a certain pointer-to-record (for example) type in your
TList, you can free it like this:
Dispose(TMyRecord(MyList[X]));
(Or FreeMem(TMyRecord(MyList[X]),SizeOfTheRecord) if you used GetMem to
allocate it)
Quote
>help would be most appriciated (never could spell that word)
Hope that helps :)
--=- Ritchie Annand