ToArray()

BlitzMax Forums/BlitzMax Programming/ToArray()

siread(Posted 2006) [#1]
Can someone explain how converting from a type list to an array works?

I am trying to convert my list to array, so that I can access the club objects via the element number. If I do this...

Local ClubArray:Object[] = TClub.List.ToArray()

How then do i access the fields of my object.

Object[n].m_Name does not work.


Fabian.(Posted 2006) [#2]
Well, try this:
Local ClubArray:TClub[] = TClub[] (TClub.List.ToArray())
You can convert an object array to an sub type array.


siread(Posted 2006) [#3]
It comiles but when I try to access ClubArray[0] I get:

"Attempt to index array element beyond array length"


siread(Posted 2006) [#4]
Oops, i was being a bit stupid.

Object[n].m_Name doesn't work but TClub(ClubArray[n]).m_Name does.

:)


Dreamora(Posted 2006) [#5]
What you would do is

TClub(Object[n]).m_name

Casting arrays does not work from what I know.

alternative would be a temporary array of object which you cast manually element by element to the final TClub array ...


Fabian.(Posted 2006) [#6]
Sorry if the array casting information was wrong, but I used this example to test it:
Strict

Local ObjectArray:Object [] = [ New T , New T , New T ] 'create an object array with lots of objects of the same type
'now think of this array as the returned value of ToArray()
Local TypeArray:T [] = T [] ( ObjectArray )'array casting
TypeArray [ 0 ].Do'access objects
TypeArray [ 1 ].Do
TypeArray [ 2 ].Do

Type T
  Method Do ( )
    Print "Object's method"
  EndMethod
EndType
And I didn't got any runtime errors; that's why I thought array casting is allowed.