An Array of Types

BlitzMax Forums/BlitzMax Programming/An Array of Types

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


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


To get the size of an array, you can just use arrayvar.length

eg:

   Local anArray:Int[200]
   Print anArray.length


Good to know, thanks!

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...

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.

Sorry, my error.

No Problem :)