QBasic/Arrays and Types
Contents |
[edit] Built-in 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
[edit] User-defined types
A user defined type allows you to create your own data structures.
TYPE asdf zxcv AS INTEGER END TYPE
You can then declare variables under this type, and access them:
DIM qwer AS asdf qwer.zxcv = 1
[edit] Arrays
An array is a collection of values stored in a single variable. While you can use arrays directly without first declaring 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.
[edit] Multidimensional arrays
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.
[edit] Non-zero base
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.
This page may need to be