getting the last item in a linked List

BlitzMax Forums/BlitzMax Programming/getting the last item in a linked List

Gillissie(Posted 2008) [#1]
This throws a type mismatch error, so how would I get the first or last object in a list? In B3D, it was as simple as calling "a = Last mytype"

Type mytype
	Field i%
End Type

Global list:TList

Local a:mytype

a = New mytype
a.i = 1
list.AddLast(a)

a = New mytype
a.i = 2
list.AddLast(a)

a = list.Last()

Print a.i



Gabriel(Posted 2008) [#2]
It throws an error because you don't typecast the object returned by Last() back into the correct type. A further error would be caused by trying to use a list which doesn't exist.

Try:

Type mytype
	Field i%
End Type

Global list:TList=New TList

Local a:mytype

a = New mytype
a.i = 1
list.AddLast(a)

a = New mytype
a.i = 2
list.AddLast(a)

a = mytype(list.Last())

Print a.i





Gillissie(Posted 2008) [#3]
hmmm. I swear I tried typecasting it by doing this in my main program (this is just a simple example program to demonstrate the issue, which is why I forgot to actually create the list).

I'll try again in my main program.

(edit) Yep, it worked. Not sure what I did differently before. I'm glad it was that simple.