TList.copy
BlitzMax Forums/BlitzMax Beginners Area/TList.copy
| ||
Hi, changing a value in the copy of a list does the same in the soruce list? Does TList.copy only create a new reference? I am really confused: Type Test Field one:Int = 1 End Type ' create a new list Local mylist:TList = CreateList() ' create an object Test with one=1 t:Test = New Test DebugLog (t.one) ' add it to list mylist mylist.AddLast(t) ' create a new list Local mylistcopy:TList = CreateList() ' and fill it with a copy of mylist mylistcopy = mylist.copy() ' Change value of t.one in copy of mylist For Local lp:Test = EachIn mylistcopy lp.one = 5 Next ' Now the value in mylist is changed to 5 ??? For Local lpp:Test = EachIn mylist DebugLog (lpp.one) Next |
| ||
Copy generates a new list with the same content (-> new TLinks as well!) |
| ||
changing a value in the copy of a list does the same in the soruce list? Correct. The objects inside the copied list are the same objects from the source list. You will need to implement your own object "clone" system to create completely new objects in your target list. Search the forum for "clone". I believe there are some code-examples which use reflection. |
| ||
While reading your anwer I came a bit nearer to my confusion. I wanted to clone a list and the objects in there to manipulate them without changing the original objects. But cloning a list only duplicates the references to the objects. So changing values of objects in one list changes the values in the other list too of course. Can anyone follow :-) ? So what I really need is a copy of the objects. Hm, Reflection? |
| ||
Ahhh, a bit faster next time Brucey. :-) |
| ||
:-) Some more info here. |
| ||
Thanks for your anwers folks! |