Lists

BlitzMax Forums/BlitzMax Beginners Area/Lists

DH(Posted 2005) [#1]
Arg, this documentation is killing me:-)

What would the syntax be to get the next object in a list.

For example:

Type MyType
	Field Value:Int
End Type

Local MyList:TList = CreateList()
Local MyStuff:MyType

For x = 1 To 5
	MyStuff= New MyType
	MyStuff.Value=5
	ListAddLast MyList,MyStuff
Next


So to get the first object in the list:
MyStuff = MyType(MyList.First())
print MyStuff.Value


However, now i want to get the next object. I dont want to loop through with eachin because I need to keep my position and come back to it.

I am trying to get NextLink to work, but I cant figure out the syntax to get what I need.

Any suggestions?


Rimmsy(Posted 2005) [#2]
You can use valueAtIndex like this:
for local i=0 to myList.count()-1
    local v:MyType=myList.valueAtIndex(i)
next



Perturbatio(Posted 2005) [#3]
MyList.First() returns an object, not a link, you need to use FirstLink() instead:

Type MyType
	Field Value:Int
End Type

Local MyList:TList = CreateList()
Local MyStuff:MyType

For x = 1 To 5
	MyStuff= New MyType
	MyStuff.Value=x
	ListAddLast MyList,MyStuff
Next

FirstLink:TLink = MyList.FirstLink()

If TLink(FirstLink) Then 
	Print MyType(FirstLink.Value()).value
EndIf

NextLink:TLink = FirstLink.NextLink()

If TLink(NextLink) Then 
	Print MyType(NextLink.Value()).Value
EndIf



DH(Posted 2005) [#4]
Thanks Rims, but TBH it looks very sloppy to use that sort of indexing when its an OO language. (no disrespect ment)

I am trying to keep everything as OO as possible, using methods and functions to handle everything. I would rather not rely on a loop or index if blitz can do it internally.

Perturbatio, that looks like it might be the solution I am after, I will impliment it and see how it does :-) Thanks!


Perturbatio(Posted 2005) [#5]
a slightly less bulky example:
Type MyType
	Field Value:Int
End Type

Local MyList:TList = CreateList()
Local MyStuff:MyType

For x = 1 To 5
	MyStuff= New MyType
	MyStuff.Value=x
	ListAddLast MyList,MyStuff
Next

Link:TLink = MyList.FirstLink()
While TLink(Link)
	Print MyType(Link.Value()).value
	Link = Link.NextLink()
Wend



DH(Posted 2005) [#6]
Event better, that's exactly what I am after! Thanks a ton!


Bot Builder(Posted 2005) [#7]
This is one of those reasons I'm writing my own...


Rimmsy(Posted 2005) [#8]
I agree with you, it does look sloppy. Now I have a better way thanks to perty.