Accessing objects in a list

BlitzMax Forums/BlitzMax Beginners Area/Accessing objects in a list

Hey,

How do i access directly the object that is being "pointed" by a link in a list?

For example:

Strict

Type TMytype	
	Field Z = 100	
End Type

Local Mylist:TList = CreateList()

Local Test01:TMytype = New TMytype
MyList.AddLast Test01

Local Test02:TMytype = New TMytype
MyList.AddLast Test02

If test02 = Mylist.Last Then Print test02.Z


In this case, I want to print "Z" field only if Test02 is the last on the list. But I get a "Types 'TMytype' and 'Object()' are unrelated". While I understand the error, I don't know how to get around it.

Also, can I access an object like Mylist.last.Z ? (it doesn't work with the "last" method).

And while I'm at it... are there any tutorials on using lists and objects? I did some basic ones, but I'm afraid I didn't find nearly as many examples under different situations as I'd like.

Thanks!

Just a couple of brackets missing there..

.List()

Strict

Type TMytype
	Field Z% = 100
End Type

Local Mylist:TList = CreateList()

Local Test01:TMytype = New TMytype
MyList.AddLast Test01

Local Test02:TMytype = New TMytype
MyList.AddLast Test02

' compare last object in list
If test02 = Mylist.Last() Print test02.Z

' cast 'object' from list to 'TMytype'
Local Test03:TMytype=TMytype(Mylist.Last())
Print Test03.z


Here are a couple of examples I wrote to aid me.
They might help you ..

Using links with lists:
Global list:TList=New TList
Global link:TLink

list.addlast "first entry"
list.addlast "second entry"
list.addlast "third entry"
list.addlast "last entry"

link=list.FirstLink() ' first
Print String(link.Value())

link=link.NextLink() ' next
Print String(link.Value())

link=link.NextLink() ' next (again)
Print String(link.Value())

link=link.PrevLink() ' previous
Print String(link.Value())

link=list.LastLink() ' last
Print String(link.Value())

link=link.PrevLink() ' previous
Print String(link.Value())

' finding an entry
link=list.FindLink("second entry")
Print String(link.Value())


Cursor UP/DOWN example for lists:
Strict

' Initialise a TList called mylist
' This is used to hold a list of strings
Global mylist:TList=New TList

' add some items to mylist
mylist.AddLast "Apple"
mylist.AddLast "Banana"
mylist.AddLast "Pear"
mylist.AddLast "Blackberry"
mylist.AddLast "Peach"
mylist.AddLast "Mango"

' Initialise a TLink called link
' Set it to point to the first item in mylist
Global link:TLink=mylist.FirstLink()

' store the string from the first item into selected$
Global selected$=String(link.Value())

' open a small graphic window
AppTitle = "Lists - Next/Previous example"
Graphics 400,300,0,30|SOFTSYNC
SetClsColor $aa,$aa,$aa


Repeat
	
	' if user presses ESCAPE break out of the loop
	If KeyHit(KEY_ESCAPE) Exit
	
	' if user presses DOWN ARROW move to next item in list
	If KeyHit(KEY_DOWN) 
		link=link.NextLink()
		If link=Null link=mylist.FirstLink()
		selected$=String(link.Value())
	EndIf
	
	' if user presses UP ARROW move to previous item in list
	If KeyHit(KEY_UP)
		link=link.PrevLink()
		If link=Null link=mylist.LastLink()
		selected$=String(link.Value())
	EndIf
	
	' If DEL pressed remove selected item
	' Now point to the first item in the remaining list
	' If the list is empty exit from the loop
	If KeyHit(KEY_DELETE)
		mylist.Remove selected$
		link=mylist.FirstLink()
		If link=Null Exit
		selected$=String(link.Value())
	EndIf

	' insert an entry AFTER the currently selected one
	If KeyHit(KEY_A)
		mylist.InsertAfterLink("Lemon "+Rand(100),link)
	EndIf
	
	' insert an entry BEFORE the currently selected one
	If KeyHit(KEY_B)
		mylist.InsertBeforeLink("Lemon"+Rand(100),link)
	EndIf

	' if user presses RETURN key show item selected
	If KeyHit(KEY_RETURN)
		Notify "Your choice is "+selected$
	EndIf
	
	' show list and highlight the 'selected' item
	Cls
	SetColor $88,$44,$00
	DrawText "UP/DOWN = select item",10,10
	DrawText "RETURN  = confirm selection",10,24
	DrawText "DEL     = Remove selected item",10,38
	DrawText "A       = Insert 'Lemon' AFTER selected item",10,52
	DrawText "B       = Insert 'Lemon' BEFORE selected item",10,66
	Local y=100
	For Local l$=EachIn mylist
		SetColor $22,$22,$22
		If l$=selected$	SetColor $ff,$ff,$ff
		DrawText l$,40,y
		y:+16
	Next

	Flip
	FlushMem
Forever
End


Hey, thanks!

You guys are always very helpful whenever my brain hits a wall and can't move forward anymore... :-)