Board index » delphi » Copying contents of a TList to another TList

Copying contents of a TList to another TList

I've come across a problem associated with TLists that I hope someone
might be able to help me with.

I have create a TList to which I have added a number of normal Pascal
records.  I have another TList to which I would like to COPY all of the
items from the first list to.

I have tried a simple assignment, like this:

var
        List1:  TList;
        List2:  TList;
begin
.
.       List2 := List1;
end;

This works, but when I destroy List1, List2 no longer points to anything.
(which makes sense).

Obviously I could run through each item in List1 and create a new record,
and then ADD it to List2, but I was hoping for an easier way.

Any comments would be appreciated.

Grant Johnson
grant.john...@acslink.aone.net.au

 

Re:Copying contents of a TList to another TList


Quote
> I have create a TList to which I have added a number of normal Pascal
> records.  I have another TList to which I would like to COPY all of the
> items from the first list to.
> Obviously I could run through each item in List1 and create a new record,
> and then ADD it to List2, but I was hoping for an easier way.

> Any comments would be appreciated.

I'm afraid there is no other solution ... ;), whatever you put into the
list (records or TObject's), you have to duplicate and insert them into the
second instance.

Re:Copying contents of a TList to another TList


On Wed, 26 Mar 1997 14:42:33 +1000, grant.john...@acslink.aone.net.au
wrote:

Quote
>I have create a TList to which I have added a number of normal Pascal
>records.  I have another TList to which I would like to COPY all of the
>items from the first list to.
>.   List2 := List1;
>This works, but when I destroy List1, List2 no longer points to anything.
>(which makes sense).

Use the assign() method.
Taken from D2 documentation:
The statement "Destination := Source" makes the Destination reference
the same object as Source, whereas "Destination.Assign(Source)" copies
the contents of the object referenced by Source into the object
referenced by Destination.

Re:Copying contents of a TList to another TList


grant.john...@acslink.aone.net.au wrote in article
<MPG.da345dcac459e65989...@news.syd.aone.net.au>...

Quote
> I've come across a problem associated with TLists that I hope someone
> might be able to help me with.

> I have create a TList to which I have added a number of normal Pascal
> records.  I have another TList to which I would like to COPY all of the
> items from the first list to.

> I have tried a simple assignment, like this:

> var
>    List1:  TList;
>    List2:  TList;
> begin
> .
> .  List2 := List1;

it's a pointer assignement and not a copy of the data of the List1 instance
!!!
so List1 and List2 are 2 pointers egal

I think you must do the following
first allocate memory for List2
List2:=TList.Create;

Copy the data  from List1 to List2 by the following assignation
List2.Assign(List1)
i don't have try it but it must work

be carefull because List2.Items[1] is still a pointer egal with
List1.Items[1]

good luck

Quote
> end;

> This works, but when I destroy List1, List2 no longer points to anything.
> (which makes sense).

> Obviously I could run through each item in List1 and create a new record,
> and then ADD it to List2, but I was hoping for an easier way.

> Any comments would be appreciated.

> Grant Johnson
> grant.john...@acslink.aone.net.au

Other Threads