You would probably be better off descending your TPics from TObjectList.
 
TObjectList has a boolean parameter into it's create constructor which says whether objects are "owned" by the TObjectList. If they are owned then they are automatically freed as you are suggesting.
 
David.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Jason Coley
Sent: Tuesday, 25 February 2003 12:20 PM
To: Multiple recipients of list delphi
Subject: RE: Re: [DUG]: Using TList

Could I do something like:

 

procedure TPics.PutPic(index: Integer; Pic: TPicture);

begin

  TPicture(items[index]).free; 

  inherited Put(index, Pic);

end;

 

 

to make it so that the old object gets freed properly?

 

Jay

 

-----Original Message-----
From: Todd Martin [mailto:[EMAIL PROTECTED]
Sent: Tuesday, 25 February 2003 11:24 a.m.
To: Multiple recipients of list delphi
Subject: Re: [DUG]: Using TList

 

Looks okay to me. Although you don't really need the Remove() and IndexOf() methods, as these just call the TList methods anyway.

 

Also, GetPic() could just call the inherited Get() method and cast the result as TPicture. In your GetPic() and PutPic() methods there's no need to cast with the Pointer type as TPicture is just a pointer anyway.

 

eg.

procedure TPics.PutPic(index: Integer; Pic: TPicture);

begin

  inherited Put(index, Pic);

end;

 

function TPics.GetPic(index: Integer): TPicture;

begin

  result := TPicture(inherited Get(index));

end;

 

Personally, I think the Put() method is very scary, since it can easily lead to memory leaks when the list holds the only pointer to an object. In other words, I usually make the Items property read only.

 

Cheers,

Todd.

----- Original Message -----

From: Jason Coley

Sent: Tuesday, 25 February 2003 10:50

Subject: [DUG]: Using TList

 

Is the below declaration of a TList that holds TPictures?

 

 

 

  TPics = class(TList)

  private

    procedure PutPic(index: Integer; Pic: TPicture);

    function GetPic(index: Integer): TPicture;

  public

    property Items[Index: Integer]: TPicture read GetPic write PutPic; default;

    function Add(Pic: TPicture): integer;

    function Remove(Pic: TPicture): integer;

    function IndexOf(Pic: TPicture): Integer;

    procedure Insert(Index: Integer; Pic: TPicture);

  end;

 

 

implementation

 

 

procedure TPics.PutPic(index: Integer; Pic: TPicture);

begin

  inherited Put(index, pointer(Pic));

end;

 

function TPics.GetPic(index: Integer): TPicture;

begin

  result := TPicture(pointer(inherited items[index]));

end;

 

function TPics.Add(Pic: TPicture): Integer;

begin

  result := inherited add(Pic);

end;

 

function TPics.Remove(Pic: TPicture): Integer;

begin

  result := inherited remove(Pic);

end;

 

function TPics.IndexOf(Pic: TPicture): Integer;

begin

  result := inherited indexOf(Pic);

end;

 

procedure TPics.Insert(Index: Integer; Pic: TPicture);

begin

  inherited insert(index, Pic);

end;

 

thanks

Jay

Reply via email to