Accessing List Objects

BlitzMax Forums/BlitzMax Beginners Area/Accessing List Objects

SoggyP(Posted 2006) [#1]
Hello.

I've a Type called Objects_Type, a list of said Objects called ObjectsList where I've added above objects using ObjectsList.AddLast(obj) (where obj is local Objects_Type).

When I try to access the list using for..eachin everything is fine, eg
for local obj:Objects_Type = eachin ObjectsList
  print obj.Id
next


However, if I use :
local obj:Objects_Type = ObjectsList.first()

I get the error Cannot convert Object Type to Objects_Type.

Can someone point out where I'm going wrong.

Goodbye.


LarsG(Posted 2006) [#2]
I'm not sure, but try casting ObjectList.first() to a Objects_type type, like this:
local obj:Objects_Type = Objects_Type(ObjectList.first())



SoggyP(Posted 2006) [#3]
Hello.

Perhaps that what it is. I've tried casting but perhaps used wrong (c) syntax:
local obj:Objects_Type = (Objects_Type)ObjectsList.first()


I could have sworn that was correct.

Goodbye.


Luke.H(Posted 2006) [#4]
Check that the first object is "Objects_Type" not some other type, which cannot be cast to "Objects_Type".

EachIn will skip objects that are not "Objects_Type"


tonyg(Posted 2006) [#5]
Global mylist:Tlist=CreateList()
Type mytype
  Field x
End Type
For x = 1 To 100
  my:mytype = New mytype
  my.x = x
  ListAddLast(mylist,my)
Next
myfirst:mytype=mytype(mylist.first())
Print myfirst.x



SoggyP(Posted 2006) [#6]
Hello.

Ta, all. Me and my multilingual incompetency :o)

Goodbye.


tonyg(Posted 2006) [#7]
Taking what Luke said...
Global mylist:Tlist=CreateList()
Type mytype
  Field x
End Type
Type notmytype
  Field x
End Type
Rem
my1:notmytype = New Notmytype
my1.x=9999
ListAddLast(mylist,my1)
End Rem
For x = 1 To 100
  my:mytype = New mytype
  my.x = x
  ListAddLast(mylist,my)
Next
If mytype(mylist.first()) 
   myfirst:mytype=mytype(mylist.first())
   Print "First *IS* a mytype"
   Print "Myfirst.x : " + myfirst.x
Else
   Print "First is not a mytype"
EndIf