GLBasic User Manual

Main sections

09 Types

Using Types



Types are a way of organizing your data into structures (sometimes called records).

Using types can greatly add to the readability of your programs variables, making any problems in your code easier to debug. Properly designed types can also reduce the length of your code as you're dealing with blocks of variable data rather than each individual variable.

To define a type you simply use:
TYPE name_of_type
// type members
ENDTYPE


A TYPE is something like a blueprint of the variable you create later.

Now that you have a type created, you can add type-members to it. Think of the TYPE as a box for storing variables and the members as the drawers which you give names. These 'drawers' can be used to store either numeric or alphanumeric variables. By default they will have the value 0 for numeric fields, and the empty string - "" - for strings.


TYPE TBook
// Count with initial empty value of 0
PageCount

// Number with initial preset value
CoverColor = RGB(128,64,0)

// String with initial empty value = ""
Title$

// String with initial preset value
Author$ = "John Doe"
ENDTYPE


You can also pack arrays into a TYPE. Be aware that these arrays are not initialized by default (DIM name[0]). You can however set a standard size so when creating an instance of a type (see later) the array is already dimensioned. To do this, put a number in the brackets when defining the array.

Of course you can use DIM and REDIM on such an array at a later point.

TYPE TCinema
// an array, no DIM
sold_seats[]

// an array, dimensioned with DIM[32][32]
free_seats[32][32]
ENDTYPE


You can create either a global or local instance of a type as follows :

GLOBAL Bible AS TBook
LOCAL MobyDick AS TBook

and access the member fields of the "Bible" instance (of type BOOK) like this:

Bible.PageCount=4500
Bible.Title$ = "The Holy Bible"
Bible.Author$ = "King James"


You can pass types to a function - they will automatically be passed by reference. "By reference" means that changing one of the types' values from inside a function also changes the value in the original calling function. Arrays are also passed by reference.

// <...> TBook definition

LOCAL Bible AS TBook, Another AS TBook
MakeBible(Bible) // This passes the "Bible" TBook to
// the MakeBible function where it
// gets populated.
Another = Bible // copy 'Bible' to 'Another'

FUNCTION MakeBible: any AS TBook
any.PageCount=4500
any.Title$ = "The Holy Bible"
any.Author$ = "King James"
ENDFUNCTION



Starting the real fun, you can nest TYPES:

TYPE TWorker
Name$
Annual_Wage
ENDTYPE

TYPE TBook
PageCount
CoverColor = RGB(128,64,0)
Title$
Author$ = "John Doe"
ENDTYPE

TYPE TLibrary
Books[] AS TBook
Employees[12] AS TWorker
ENDTYPE

LOCAL MyLibrary AS TLibrary

MyLibrary.Employees[1].Name$="Fred"


And you can use a type as a return value:

LOCAL Bible AS TBook
Bible = MakeBook("The Holy Bible", 4500)

FUNCTION MakeBook AS TBook: name$, nPages
LOCAL temp AS TBook
temp.Title$ = name$
temp.PageCount = nPages
RETURN temp
ENDFUNCTION


If you leave out the RETURN statement from such a function a new instance of that type (here TBook) will be returned that has the standard values assigned.

You can also have an array of TYPEs
TYPE TWorker
Name$
Annual_Wage
ENDTYPE

GLOBAL Employees[] AS TWorker
DIM Employees[5]

You can easily make that a multi-dimensional array by
GLOBAL Employees[] AS TWorker
DIM Employees[5][4]


If you have an array of TYPEs, you can remove items with DIMDEL and DELETE or DIMPUSH new elements to the back of the array.

If you want to loop through each element in an array of TYPEs, use the FOREACH command.

The last value of an array is either a[LEN(a[])-1], or simply a[-1].

See also...