Types

Blitz3D Forums/Blitz3D Beginners Area/Types

Since you now understand arrays...

Types can be thought of as special arrays that can hold more than just one type of data per element. (They are not actually arrays, but let's just pretend that they are for now.)

For example:
Dim AnArray(0)
is an array that can contain one integer as a value.
Dim AnArray$(0)
is an array that can contain one string as a value.

Creating an array that can hold more than one integer per element is easy (create a two-dimensional array):
Dim AnArray(0,3)
Now you have an array that can hold 4 integers (0-3) per element.

Creating an array that can hold more than one string per element is easy as well:
Dim AnArray$(0,3)
Now you have an array that can hold 4 strings per element.

But what if you wanted your array to be able to hold both kinds of values (integers AND strings)? You can't create an array that can hold both types of variables.

But you can use Types to achieve this.
Type AType
  Field AnInteger
  Field AString$
End Type
Now if you create an instance of that type:
thistypeinstance.AType = New AType
You can assign both your integer and string values to it:
thistypeinstance\AnInteger = 1435
thistypeinstance\AString$ = "Hello World"
The reason I am comparing them to arrays is because like arrays (which have multiple elements), types can have multiple elements too. You just create another instance of it.
thistypeinstance.AType = New AType
Now we could assign values to this new instance if we wanted to:
thistypeinstance\AnInteger = 348
thistypeinstance\AString$ = "Hi Mom!"
PLEASE NOTE that since we created a New type instance and assigned it to the same variable 'thistypeinstance', that we can no longer access the first instance we created using that variable any more(it now points to the second instance we created).
In order to do that, we have to iterate through the type list to retrieve it:
For thistypeinstance.AType = Each AType
  Print thistypeinstance\AnInteger
  Print thistypeinstance\AString$
Next
Which would produce the results:
1435
Hello World
348
Hi Mom!