how to get next object in a list

BlitzMax Forums/BlitzMax Beginners Area/how to get next object in a list

Im trying to use blitz3d type access as in the 'blitzmax tutorial forum'
bit when I use link.findnext()
it says trying to access field of null object





is there another way to get the next object in a list?


EDIT= this works
Local link:TLink = the_List.FindLink(Self)

this causes a crash=
Local link:TLink = the_List.FindLink(Self).nextlink()


The problem is that you need to check if the link provided by FindLink() exists or not.

edit - simple example (I hope...)

Local the_list:TList = New TList

Type obj
	Field value$
End Type

Local myobj:obj[20]
For i = 1 To 10
	
	myobj[i - 1] = New obj
	myobj[i - 1].value="Hallo "+String(i)
	
	the_list.addlast myobj[i - 1]

Next

For Local iaa:obj = EachIn the_list
	Print iaa.value
Next

Local link:TLink = the_List.FindLink(myobj[1]) 
Local newLink:TLink

If link
	Print "Link exists "+obj(link.value()).value
	newlink = link.NextLink() 
	
	Print "<"+obj(newlink.value()).value+">"
	
Else
	Print "Link doesn't exist"
End If


Try to change FindLink(myobj[1]) with FindLink(myobj[11]) - this link doesn't exist - but you catch the possible error

From the user help:

'Author:Assari
'FirstLink(), Value() and NextLink()
'Note that Value() and NextLink Methods from Type TLink
'Whereas FirstLink() is a method from Type TList
SuperStrict

Local MyList:TList = CreateList() 
MyList.AddFirst("A")
MyList.AddFirst("B")
MyList.AddFirst("C")

Local NLink:TLink = MyList.FirstLink() 'get first Link
Repeat
   Print NLink.Value().ToString() 'Need to convert value from object to string
   NLink = NLink.NextLink()       'Move to the next link in list
Until Nlink = Null                'Null means we've reached the end of the list

================
Output
C
B
A


Hello.

Be cautious with findLink(). The search as O(n) speed.
If you call findlink() often on a large list your fps fall down.

Use a tmap to save all tlinks . Search self in tmap and call nextlink() only in not null result. The same operation take O(1) speed at cost of memory.

Bye,
Paposo