How to assign a TStringList to another?

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
LVL 2
SupWangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zebadaCommented:
MyListBakList.Text := MyListBox.Items.Text
0
ulamtheluckyCommented:
This also works...

MyListBox.Items.AddStrings(MyListBakList);
0
edeyCommented:
better yet, TMyStrings.assign(someOtherStringList);

GL
Mike
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MotazCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DumaniCommented:
MyListBakList := TStringList.Create;
MyListBakList.Assign(MyListBox.Items);

that's the way to Assign...
Regards,
Atilla
0
Igor UL7AAjrSenior developerCommented:
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.
0
zebadaCommented:
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 :)
0
SupWangAuthor Commented:
Hi all,
Thanks.
Because Motaz give me more explaintion, so I want to give the points to him.

Thanks, supwang
0
MotazCommented:
Thanks Supwang.

Motaz
www.geocities.com/motaz1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.