An Array of Types

BlitzMax Forums/BlitzMax Programming/An Array of Types

maverick69(Posted 2005) [#1]
As all of you know, in Blitz its possible to use arrays.

Doesn't it work with types? I have written the following code and the size of the resized array is always 0:


Type MyType
	Field x
	Field y
End Type

Local a:MyType[200]

count=0
For Local b:MyType = EachIn a
	count=count+1
Next
DebugLog "Size: "+count
End



maverick69(Posted 2005) [#2]
I'm sorry. After I had written the post above I've found my mistake. Have forgotten to initilaze the fields in the array. The following works:

Type MyType
	Field x
	Field y
End Type

Local a:MyType[200]

For i = 0 To 199
	a[i] = New MyType
Next

count=0
For Local b:MyType = EachIn a
	count=count+1
Next
DebugLog "Size: "+count
End



Robert(Posted 2005) [#3]
To get the size of an array, you can just use arrayvar.length

eg:

   Local anArray:Int[200]
   Print anArray.length



maverick69(Posted 2005) [#4]
Good to know, thanks!


CoderLaureate(Posted 2005) [#5]
Instead of using:
Local a:MyType[200]

For i = 0 To 199
	a[i] = New MyType
Next


You could use
Local a:MyType[] = new MyType[200]

Just one line...


McFox(Posted 2005) [#6]
CoderLaureate >

This :
Local a:MyType[] = new MyType[200]


Just doesn't work, take a look at that :

Type myType
	Field A:Int
	Field B:Int
EndType

Local Locations:MyType[] = New MyType[10]

Locations[0].A = 10
Locations[0].B = 25
Locations[1].A = 12
Locations[1].B = 32

Print Locations[0].A
Print Locations[0].B
Print Locations[1].A
Print Locations[1].B


I got a Unhandled Exception: Attempt to access field or method of Null object
It seems that you must do a ForNext loop.


CoderLaureate(Posted 2005) [#7]
Sorry, my error.


McFox(Posted 2005) [#8]
No Problem :)