Types

Show Index

A fornote about this page:  The word TYPE is used many times on this page.   In an effort to avoid confusion, when it is used in reference to the BlitzBASIC command Type, it will be written in gold text.  When it is not used in reference to the command Type, it will be written in plain text.

 

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.  (Remember that Blitz arrays are zero-based)

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 (just create a two-dimensional array):

Dim AnArray(0,3)

Now you have an array that can hold 4 integers (0-3) per element.

And the same is true for an array that can hold more than one string per element:

Dim AnArray$(0,3,2)

Here is an array that can hold 12 strings (1x4x3) per element.

 

But what if you wanted your array to be able to hold BOTH integers AND strings? You can't.  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 access the first instance (and the rest) 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!

 


Now, if you find yourself still a bit confused after all of that, it might help to try to explain what is going on in the above code snippets.

First of all, this code:

Type AType
  Field AnInteger
  Field AString$
End Type

sets up a Type called AType (in this example).  It is being set up with two Fields (AnInteger and AString$) that will be able to contain data.  So, each INSTANCE of this Type, will be able to store two different kinds of things about that instance.

A better example might be one like this:

Type Student
  Field ID	;student ID number
  Field Name$	;the students name
  Field Address$;mailing address
  Field Phone$	;phone number
  Field GPA#	;grade point average
End Type

Here, I've created a type of student.  Every student needs an ID number.   Every student also has a name, address, phone number, and a GPA.  Note that Types allow me to store different kinds of data (integers, strings and floats) all together.

Now typing in the above code doesn't actually create any instances of the Student Type.  It just tells the computer what types of data the Student Type can hold.  To create our first student (instance of Student) we need to type in something similar to the following code:

TypeInstanceHandle.Student = New Student

The above code will create an new instance of the Student Type and assign that instances handle to the variable TypeInstanceHandle (so that we can reference it later).  Now, I could have named TypeInstanceHandle anything I wanted to.  In fact, I prefer naming it like so:

thisStudent.Student = New Student

The reason I like to name the variable using the word 'this' is because it makes it more readable (and understandable) later when I have to assign values to it's Fields.  You will see how shortly.

A quick note about the variable 'thisStudent.Student'.  Why do I have to place '.Student' after the variable name when creating a new instance of it?

The reason is because Blitz needs to know what kind of variable 'thisStudent' is.   It is exactly the same behavior when we create any other new variables in Blitz.   Every time we create a new variable we must let Blitz know what kind of variable it is by assigning an identifier to it.
If it's an integer then we need to assign a % to it (or nothing at all).  If it's a string then we need to assign a $ to it.  If it's a floating point variable, then we need to assign a # to it.

So, keeping with that behavior, if it's a Type, then we need to assign a Type identifier to it (in this case '.Student').

Having said that; since you only need to let Blitz know what kind of variable a variable is the FIRST time you use that variable (for example, mystring$ can later be written in your code as just mystring), the same is true for Types.   The first time we use the variable 'thisStudent' we have to include the identifier '.Student' but every time thereafter, we only need to use the variable name 'thisStudent'.

First time we use the variable 'thisStudent' in our code:

thisStudent.Student = New Student

Every time thereafter:

thisStudent = New Student

Makes sense?  If it doesn't, don't worry...  You can always still use the type identifier with the variable.  In other words, if you didn't understand this explanation, then just continue to write/use all of your Types like this:

thisStudent.Student = New Student

Getting back on track, lets create a new instance of the Student Type and then assign values to that instances Fields.

thisStudent.Student = New Student

thisStudent\ID = 00001
thisStudent\Name$ = "John Doe"
thisStudent\Address$ = "123 Anystreet, Anytown, USA"
thisStudent\Phone$ = "555-1212"
thisStudent\GPA# = 3.2

This will create the first instance of the Student Type and add it to the Student Type list.

And now to add another student to the Student Type list:

thisStudent = New Student

thisStudent\ID = 00002
thisStudent\Name$ = "Jane Doe"
thisStudent\Address$ = "456 Herstreet, Hertown, UK"
thisStudent\Phone$ = "01632 960008"
thisStudent\GPA# = 3.7

Can you see how naming the variable using the word "this" tends to make sense because we are no longer talking about the first student anymore?  We are always talking about "this" student.  In this fashion we can REUSE the variable name 'thisStudent' over and over and it remains very readable.

After the above code was executed, the Student Type list would contain two instances of the Student Type.

If we wanted to recall what the last students name was, we could simply type something like:

A_Students_Name$ = thisStudent\Name$

But if we wanted to recall what the first students name was, we wouldn't be able to do it by using the 'thisStudent' handle anymore since it now is pointing to the last student that was created (because of this line: thisStudent = New Student when we created Jane Doe).

We could try retrieving the first students name by using a modified version of a For-Next loop, the For-Each loop.   We could then look for their ID number or any other type of information that will allow us to find them in the list.

For thisStudent = Each Student
  If thisStudent\ID = 00001
    A_Students_Name$ = thisStudent\Name$
    Exit
  EndIf
Next

The above code would search through the entire Type list of Students (all two of them, John Doe and Jane Doe) and when it finds the student with the ID of 00001, it stores the students name (in A_Students_Name$) and then Exits the loop since it doesn't need to search any furthur.

 

If you've reached this page and there's no index on the left, Click here to show the Index Page