QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The DIM statement is used to declare a variable or a list of variables as a specified data type or to dimension $STATIC or $DYNAMIC Arrays.

Syntax

To declare variables:

DIM [SHARED] variable[{suffix AS [_UNSIGNED] type}] [, variable2…]]

To declare arrays:

DIM [SHARED] array([lowest% TO] highest%])[{suffix AS [_UNSIGNED] type}] [, variable2…]

QB64 Alternative Syntax:

DIM [SHARED] AS [_UNSIGNED] typevariable [, variable2…]

DIM [SHARED] AS [_UNSIGNED] typearray([lowest% TO] highest%]) [, array2(elements)…]

Description

Example(s)

Defines Qt variable as a one byte fixed length string.


DIM Qt AS STRING * 1 

Dimensions and types an array.


DIM Image(2000) AS INTEGER

Dimensions array with an INTEGER type suffix.


DIM Image%(2000)  

Dimensions a range of Arrays elements as SHARED integers.


DIM SHARED Image(1 TO 1000) AS INTEGER 

Dimensions variable as an Arrays of 8 elements of the type UNSIGNED BIT.


DIM bit(8) AS _UNSIGNED _BIT 

QB64 is more flexible than QBasic when it comes to “Duplicate Definition” errors. The following code does not error:


x = 1 'x is a SINGLE variable
PRINT x
DIM x AS LONG
PRINT x 

Explanation: The SINGLE variable can be differentiated from the LONG x variable by using suffixes like x! or x& in later code.

The following code will create a “Name already in use” status error in QB64 when the variable types are the same.


x = 1 'x is a SINGLE variable
PRINT x
DIM x AS SINGLE
PRINT x 

Explanation: QB64 gives an error because the creation of the new variable would make referring to the existing one impossible.

Using QB64’s alternative syntax to declare multiple variables/arrays of the same type.


DIM AS LONG w, h, id, weight, index 'all of these variables are created as type LONG
DIM AS SINGLE x, y, z               'all of these variables are created as type SINGLE

See Also