Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating a TList of objects

377 views
Skip to first unread message

Chris Cauchi

unread,
Nov 15, 1999, 3:00:00 AM11/15/99
to
I want to create a list of objects, in this case TBitmaps. The number of
bitmaps needed are not known until runtime. All the code I've seen deals
with adding a known number to the list... I'm losing my hair trying to
figure out how to dynamically allocate any number of TBitmaps in a loop
and add them to the list! Do I have to use a dynamic array (I'm using D2
so I have to do this 'the old way') to create the actual bitmaps
seperate from the TList, or is there a more graceful way of doing
this?!?

Chris Cauchi

David Reed

unread,
Nov 15, 1999, 3:00:00 AM11/15/99
to
> with adding a known number to the list... I'm losing my hair trying to
> figure out how to dynamically allocate any number of TBitmaps in a loop
> and add them to the list! is there a more graceful way of doing
> this?!?

You do not need to know the number of objects in advance when using a
TList - that's kind of the idea of a TList and its "Count" property.. Here
are quick instructions for using TList (untested..):


var
List: TList;
p: ^TBitmap; // pointer to the type of object
x: integer;

begin

// create the list
List := TList.Create;
// allocate memory for the pointer
New(p);
// assign the pointer
p^ := Image1.Picture.Bitmap;
// add to List
List.Add(p);
// use the list
Canvas.Draw(0,0,TBitmap(List[0]^));
// free the memory
for x := 0 to List.Count - 1 do
Dispose(List[x]);
List.Free;

end;


--
David Reed
Diamond Software Group, Inc
www.diamondsg.com

Chris Cauchi

unread,
Nov 16, 1999, 3:00:00 AM11/16/99
to
Thanks, I think that got me on the right track. But I'm not sure about the
pointer issues in converting it into a loop and reusing the same pointer
variable... is the code below kosher, pointer-wise?

var
bs: TBlobStream;
list: TList;
pBM: ^TBitmap;
i: integer;
begin
list:=TList.Create;
ITable.First;
for i:=0 to ITable.RecordCount-1
do begin
bs:=TBlobStream.Create(ITableBitmap,bmRead);
New(pBM);
pBM^:=TBitmap.Create;
pBM^.LoadFromStream(bs);
list.Add(pBM^);
bs.Free;
ITable.Next;
end;

And, to free the objects, do I call TBitmap(list.Items[x]).Free, or
Dispose(list.Items[x])?

Thanks again
Chris Cauchi

David Reed

unread,
Nov 17, 1999, 3:00:00 AM11/17/99
to
> list.Add(pBM^);
** change this line to
list.Add(pBM); // add the pointer, not the dereferenced pointer.

> And, to free the objects, do I call TBitmap(list.Items[x]).Free, or
> Dispose(list.Items[x])?

You have to destroy both the bitmap object *and* the pointer var. When
freeing the bitmap, dereference the pointer object using the following:

TBitmap(list.Items[x]^).Free

You were correct with the syntax for dispose, tho.. put the call in a loop
"for x := 0 to List.Count - 1", preferably in a try...finally loop as well.
Keep practicing with pointers until you completely understand their role and
how they work - they are invaluable to a windows programmer, and it'll help
many more concepts in Delphi make sense..

Chris Cauchi

unread,
Nov 17, 1999, 3:00:00 AM11/17/99
to
I've changed the storage from a TList to a TStringList, so I can lookup the
images via a text key. TStringList.AddObject wants an actual TObject rather than
a pointer. In the code below, is the Dispose properly used? I figure I should
free the memory for the pointer each iteration, and this will not affect the
actual object that was created and stored in the stringlist. Is this correct?

formatCodeBitmapsSL:=TStringList.Create;
for i:=0 to dm.FormatAllQBE.RecordCount-1
do begin
if not dm.FormatAllQBECode.IsNull
then begin
New(pBm);
pBm^:=TBitmap.Create;
pBm^.Assign(dm.FormatAllQBEGraphic);
formatCodeBitmapsSL.AddObject(dm.FormatAllQBECode.Value,pBm^);
Dispose(pBm);
end;
dm.FormatAllQBE.Next;
end;

Then to free the whole shebang, I simply need this, true?...

for i:=0 to formatCodeBitmapsSL.Count-1
do TBitmap(formatCodeBitmapsSL.Objects[i]).Free;
formatCodeBitmapsSL.Free;

Chris Cauchi

0 new messages