Declaring types

Types can contain regular variables such as floating point values, integers and strings. They can also contain arrays and other types. This section of the documentation provides several examples showing the different ways in which types can be declared.

The program that follows defines and uses a type named myType.

type myType
	a as integer
	b as float
	c as string
endtype

data as myType
data.a = 1 data.b = 1.23 data.c = "hello"
do print ( data.a ) print ( data.b ) print ( data.c )
sync ( ) loop

The type declaration for myType contains three variables named a, b and c. a is declared as an integer, b is declared as a floating point value and finally c is declared as a string. A variable named data is declared that is of type myType. This is followed by three lines that assign values to the contents of data. Then there's the loop that continually prints out the values within data

The next program shows how a type can contain an array.

type myType
	a as string [ 4 ]
	b as integer [ 4 ]
endtype

data as myType
data.a [ 1 ] = "a" data.a [ 2 ] = "b" data.a [ 3 ] = "c" data.a [ 4 ] = "d"
for i = 1 to 4 data.b [ i ] = i * 5 next i
do for i = 1 to 4 print ( data.a [ i ] + " " + str ( data.b [ i ] ) ) next i
sync ( ) loop

A string array named a is declared within the type declaration along with an integer array named b. The way they are declared is just the same as when dealing with regular arrays, except these arrays are contained within myType. This is followed by a few lines that assign values to the arrays within data. Then the loop simply prints out the contents of data.a and data.b

Types can also contain other types, as displayed in this program.

type myTypeA
	score as integer
	names as string [ 4 ]
endtype

type myTypeB sub as myTypeA value as float endtype
data as myTypeB
data.sub.score = 1 data.sub.names [ 1 ] = "a" data.sub.names [ 2 ] = "b" data.sub.names [ 3 ] = "c" data.sub.names [ 4 ] = "d" data.value = 1.45
do print ( data.sub.score )
for i = 1 to 4 print ( data.sub.names [ i ] ) next i
print ( data.value )
sync ( ) loop

The type myTypeA is declared, which is then followed by the declaration for myTypeB. This type declares a variable sub that uses the type myTypeA. The next few lines beneath this show how to access a type within a type.

The final program shows how you can have an array of a type.

type playerType
	score as integer
	name as string
endtype

players as playerType [ 3 ]
players [ 1 ].score = -1000 players [ 1 ].name = "lee" players [ 2 ].score = 100 players [ 2 ].name = "paul" players [ 3 ].score = 500 players [ 3 ].name = "rick"
do for i = 1 to players.length print ( players [ i ].name + " = " + str ( players [ i ].score ) ) next i
sync ( ) loop

To specify that a variable using a type is going to be an array just add on the opening bracket, the size of the array and lastly the closing bracket, just like you would do with a regular floating point, string or integer array.

The variable players is treated like a regular array, so it's possible to use the likes of length and sort with it, as shown in the for loop that iterates from 1 to the size or length of the array.