QBasic/Arrays and Types
QBasic has five built-in types: INTEGER (%), LONG(&), SINGLE(!), DOUBLE(#) or STRING($). You can declare a variable to one of these types by adding the type character after the variable name.
An alternate method of declaration is using the DIM statement:
DIM NAME AS STRING
A user defined type allows you to create your own data structures. Please note that custom types are similiar to arrays.
TYPE playertype NAME AS STRING score AS INTEGER END TYPE
You can then declare variables under this type, and access them:
DIM playername AS playertype playername.NAME = "Bob" playername.score = 92
This above example shows how a custom type can be used for maintaining data, say on a player who plays a game.
An array is a collection of values stored in a single variable. Unless you DIM them, they are limited to 10 elements on each dimension.
By default, arrays in QBasic are static in size and cannot be changed later in the program. Code that will set up this type of array is as follows:
DIM myArray(10) AS TYPE
TYPE can be any of the built in QBasic or user-defined type. If this is not specified, the array takes the default type for the variable.
By issuing the Meta Command '$DYNAMIC at the beginning of your program you can cause your arrays to be dynamic:
' $DYNAMIC DIM myDynamicArray(5) AS INTEGER REDIM myDynamicArray(10) AS INTEGER
This is now perfectly legal code.
To free up space occupied by an array, use the ERASE statement.
An array isn't restricted to one dimension - it's possible to declare an array to accept two parameters in order to represent a grid of values.
DIM housenames(25,25) AS STRING
You cannot use the REDIM statement to change the number of dimensions on the array, even with dynamic allocation.
In most languages, arrays start at the value 0, and count up. In basic, it's possible to index arrays so that they start at any value, and finish at any other value.
DIM deltas(-5 TO 5)
You can change the default lower bound with the OPTION BASE statement.