You could, but how do you know the picture object at index isn't being referenced elsewhere in your application?
Better to just remove the object and only free it if you don't need it anymore.
 
Todd.
----- Original Message -----
Sent: Tuesday, 25 February 2003 12:20
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