QBasic/Arrays and Types

Built-in Types
QBasic has five built-in types: INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#) float and STRING($). QB64 has two more built-in types: _INTEGER64 (&&) and _FLOAT (##)

Implicit declaration is by adding the type character to the end of the variable name (%, &, !, #, $, &&, ##). See QBasic/Basic math for more.

Explict declaration is by using the DIM statement before first use: If you do not use either implicit or explicit declaration, QBASIC interpreter assumes SINGLE type.

User-defined type
A user defined type allows you to create your own data structures. Please note that custom types are similar to arrays.

You can then declare variables under this type, and access them:

This above example shows how a custom type can be used for maintaining data, say on a player who plays a game.

Array
An array is a collection of values stored in a single variable. A STRING is an array of characters (so, for example, char$(1) means 1st character in string char$). Arrays of numbers should be defined using the DIM instruction (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:

TYPE can be any of the built in QBasic (INTEGER, LONG, SINGLE, DOUBLE, STRING) or user-defined type. If this is not specified, the array takes the Type defined by the variable name suffix - INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#), STRING($) - or INTEGER if none.

WARNING: If your data Type is string, DIM string(10) defines a SINGLE string of 10 characters, NOT 10 strings of arbitary length ! (10 strings of up to 128 chars each would be defined as DIM string(10,128)

By issuing the Meta Command '$DYNAMIC at the beginning of your program you can cause your arrays to be dynamic:

This is now perfectly legal code.

To free up space occupied by an array, use the ERASE statement.

Multidimensional array
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.

You cannot use the REDIM statement to change the number of dimensions on the array, even with dynamic allocation.

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.

You can change the default lower bound with the OPTION BASE statement.