How to assign a TStringList to another?

SupWang
SupWang used Ask the Experts™
on
Hi all,
How to assign a TStringList to another?

Why can't do this?
----------------->
  MyListBakList := TStringList.Create;
  MyListBakList := MyListBox.Items;      //MyListBox: TListBox
-----------------<


I just can do this by:
----------------->
  MyListBakList := TStringList.Create;
  for Counter := 0 to MyListBox.Items.Count - 1 do
  begin
    MyListBakList.Add(MyListBox.Items[Counter]);
  end;
-----------------<

Regards, supwang
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
MyListBakList.Text := MyListBox.Items.Text
This also works...

MyListBox.Items.AddStrings(MyListBakList);

Commented:
better yet, TMyStrings.assign(someOtherStringList);

GL
Mike
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Commented:
I'm with edey,
Assign method used to copy similar properties in a bit-different objects such as TStringList and TStrings, also it make a new copy of object data instead of making two object references points to the same instance, example:

List2:= TStringList.Create;
List1:= TStringList.Create;
List2.Add('Hello');
List1:= List2;

By using this assignment List2 and List1 will points to the same memory location, if you free list1, list2 will be freed. List1:= TStringList.Create means no thing in the previous example.

By using Assign, a copy of List2 data will be moved to a seperate location that already pointed to by List1:

List2:= TStringList.Create;
List1:= TStringList.Create;
List2.Add('Hello');
List1.Assign(List2);

In this case freeing or modifing any one of instances (List1, List2) does not affect the other one.

Motaz

Commented:
MyListBakList := TStringList.Create;
MyListBakList.Assign(MyListBox.Items);

that's the way to Assign...
Regards,
Atilla
Igor UL7AAjrSenior developer

Commented:
Hi SupWang,

here is a good answer from zebada,
>>MyListBakList.Text := MyListBox.Items.Text

Dumani, new expert wellcome. Try do not propose an "answer", just write a "comment". Let questioner to decide who will be graded. Please, read this article:
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20081052#tip

---------
Igor.

Commented:
SupWang,

Even though my comment works - you probably should give the points to edey - that comment is far more elegant. I learned something today as well :)

Author

Commented:
Hi all,
Thanks.
Because Motaz give me more explaintion, so I want to give the points to him.

Thanks, supwang

Commented:
Thanks Supwang.

Motaz
www.geocities.com/motaz1

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial