How to copy an object from TList?

BlitzMax Forums/BlitzMax Programming/How to copy an object from TList?

BlackSp1der(Posted 2006) [#1]
Hi, I'm working on Tlist of Tlist of Tlist .....
and I don't know how to copy an object without have it connected with the previous one.
I can declare a temporal variable and then copy all the values to the temporal variable, and later add it to the list. but what about if I want to copy all the tree lists?.

check the example below. How to copy an object without have it linked to the previous one?, and how to copy all the SUBLIST 1 to another SUBLIST without the need to copy all the data to temporal variable using for-eachin.


Type TMain
	Field SubList:Tlist=CreateList()
EndType

	Type TSub
		Field ExtraList:Tlist=CreateList()
	EndType

		Type TExtra
			Field X:Int
			Field Y:Int
		EndType


Global list:TList=CreateList()

list.AddLast New TMain

TMain(list.last()).SubList.AddLast New TSub
TMain(list.last()).SubList.AddLast New TSub


Local NewData:TExtra

NewData=New TExtra
NewData.X=10
NewData.Y=11
TSub( TMain( list.last() ).SubList.first() ).ExtraList.AddLast NewData

NewData=New TExtra
NewData.X=20
NewData.Y=21
TSub( TMain( list.last() ).SubList.last() ).ExtraList.AddLast NewData

showlists()

Print "extract the first object of sublist/extralist"
Print "and copying it to the last sublist/extralist"
NewData=TExtra( TSub( TMain( list.last() ).SubList.first() ).ExtraList.first() ) 'last object from extralist
TSub( TMain( list.last() ).SubList.last() ).ExtraList.AddLast NewData

showlists()


Print "changing the first value of extralist to X999 Y888"
NewData=TExtra( TSub( TMain( list.last() ).SubList.first() ).ExtraList.first() )
NewData.X=999
NewData.Y=888

showlists()


Function showlists()
	Print "---------------"; pos1=0
	For main:TMain=EachIn list; pos1:+1
		Print "main "+pos1; pos2=0
		For sub:TSub=EachIn main.SubList; pos2:+1
			Print "~tsub "+pos2; pos3=0
			For extra:TExtra=EachIn sub.ExtraList; pos3:+1
				Print "~t~textra "+pos3+" =  X:"+extra.X+"  Y:"+extra.Y
			Next
		Next
	Next; Print "---------------"
EndFunction