using TList for integers?

BlitzMax Forums/BlitzMax Beginners Area/using TList for integers?

puredave(Posted 2009) [#1]
have a look at this:

Local sList:TList=CreateList()
ListAddLast sList,"test"
ListAddLast sList,"text"
ListAddLast sList,"okay"
For Local s:String=EachIn sList
Print s
Next
End

works okey dokey no problem

but if i do this:

Local iList:TList=CreateList()
ListAddLast iList,12
ListAddLast iList,6
ListAddLast iList,78
For Local i:Int=EachIn iList
Print i
Next
End

no can do!

how can i use lists for integers? i know how arrays work but was doing some testing then found that i couldnt use integer values in a list. am i missing something or is it impossible?


Gabriel(Posted 2009) [#2]
You can't. Integers are not objects, and strings are. You would have to build a container object.


puredave(Posted 2009) [#3]
okay thanks for that, thought i was doing something wrong :)


Czar Flavius(Posted 2009) [#4]
This shows two ways, using Strings, or a container object. I used the object-orientated style of using lists, but you can use ListAddLast instead without any problem.

Local list1:TList = New TList
list1.AddLast("1")
list1.AddLast("2")
list1.AddLast("3")

For Local i1:String = EachIn list1
	Print i1
	Print Int(i1) * 2
Next

Type TInt
	Field v:Int

	Function Create:TInt(value:Int)
		Local i:TInt = New TInt
		i.v = value
		Return i
	End Function
End Type

Local list2:TList = New TList
list2.AddLast(TInt.Create(1))
list2.AddLast(TInt.Create(2))
list2.AddLast(TInt.Create(3))

For Local i2:TInt = EachIn list2
	Print i2.v
	Print i2.v * 2
Next



GfK(Posted 2009) [#5]
Just as a side-note, TMaps are incredibly useful. Last year I'd never used them before, so I asked. Use them all the time now. They're better suited to many tasks than TLists are.


slenkar(Posted 2009) [#6]
yeah its lucky you asked, that thread really helped me to understand tmaps as well