Board index » delphi » How to make a really clone for TStringList?

How to make a really clone for TStringList?

I have a TStringList type variable A and B,the A use AddObject to add some
object correspond with some text.
I want copy A to B,so write down the code "B:=A;".But I notice B is not a
real clone,it's the only pointer to A.When I modify B,A is modified.Can
someone help me?
 

Re:How to make a really clone for TStringList?


lecon <le...@yeah.net> schrieb in im Newsbeitrag: 3b9c7ac5_2@dnews...

Quote
> I have a TStringList type variable A and B,the A use AddObject to add some
> object correspond with some text.
> I want copy A to B,so write down the code "B:=A;".But I notice B is not a
> real clone,it's the only pointer to A.When I modify B,A is modified.Can
> someone help me?

Hi,
use B.Assign(A);
See help for detailed description.

HTH
(
--
Florian Haag
ICQ: 114504497
http://private.addcom.de/h/haag/fh/

Re:How to make a really clone for TStringList?


Quote
On Mon, 10 Sep 2001 16:37:05 +0800, "lecon" <le...@yeah.net> wrote:
>I have a TStringList type variable A and B,the A use AddObject to add some
>object correspond with some text.
>I want copy A to B,so write down the code "B:=A;".But I notice B is not a
>real clone,it's the only pointer to A.When I modify B,A is modified.Can
>someone help me?

To clone objects, use Assign (which depends on the object decending
from TPersistent btw). So:

B.Assign(A);

- Asbj?rn

Re:How to make a really clone for TStringList?


Thanks regard!
But Assign method isn't the key.
B.Assign(A);Only the B's Strings is clone,but not Objects,all Objects of B
is the Pointers to A's corresponding Objects.I wrote following code to test
it.

//***************************************************
//First put three ListBox on form,named lbCatalog,lbItems1,lbItems2
//and a Button,named btnDel.
//sBackUp is the Backup of  lbCatalog's Strings and Objects
//***************************************************
procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
sBackUp:=TStringList.Create;
for i:=0 to 9 do begin
    lbCatalog.Items.AddObject('String'+IntToStr(i),TStringList.Create);
    TStringList(ta.Items.Objects[i]).Sorted:=true;
    TStringList(ta.Items.Objects[i]).Duplicates:=dupIgnore;
    for j:=0 to 9 do

TStringList(ta.Items.Objects[i]).Add('String'+IntToStr(i)+IntToStr(j));
end;
sBackup.Assign(lbCatalog.Items);
end;

procedure TForm1.lbCatalogMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
lbItems1.Items:=TStringList(lbCatalog.items.Objects[lbCatalog.ItemIndex]);
lbItems2.Items:=TStringList(sBackup.Objects[lbCatalog.ItemIndex]);
end;

procedure TForm1.btnDelClick(Sender: TObject);
var i:integer;
begin
for i:=lbItems1.Count-1 downto 0 do
  if lbItems1.Selected[i] then
    TStringList(lbCatalog.Items.Objects[lbCatalog.ItemIndex]).Delete(i);
lbItems1.DeleteSelected;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
sBackup.Free;
end;

Re:How to make a really clone for TStringList?


Quote
In article <3b9cafc3_1@dnews>, Lecon wrote:
> But Assign method isn't the key.
> B.Assign(A);Only the B's Strings is clone,but not Objects,all Objects of B
> is the Pointers to A's corresponding Objects.

You are having the same problem again that started this thread: the Assign
copies the object references (pointers) stored in the Objects property, it
does not make copies of the objects. It cannot, the TStringlist.Assign code
has absolutely no idea what is stored in the Objects property. It may be a
simple number, it may be a pointer to some heap data, it may be an object
reference. Even if it were restricted to object references there is no
generic method to clone an object, regardless of its type. The object may
contain references to other objects and so on.

Only you, as the programmer, know what the Objects property will contain, so
you have to write the code to properly copy it. If you store objects you know
which objects these are and how to copy them.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:How to make a really clone for TStringList?


Quote
> Only you, as the programmer, know what the Objects property will contain,
so
> you have to write the code to properly copy it. If you store objects you
know
> which objects these are and how to copy them.

Yes,you are right,I rounded it.I assign each objects of destination from
each objects of source.
Thank regards!Thank all!

Other Threads