DIM

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > Data types and variables >

DIM

 

Description

 

Declare and dimension variables and/or arrays.

 

Syntax

 

Depending on what are the needs, there are may different ways to declare variables.

 

DIM VarName[(subscripts)] AS VarType [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

DIM VarName[(subscripts)], VarName[(subscripts)] AS VarType [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

DIM VarName[(subscripts)] AS VarType [ {VALUE | =} InitialValue ][, VarName[(subscripts)] AS VarType [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

DIM VarName[(subscripts)] LIKE <a string expression representing a supported base type> [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

DIM VarName[(subscripts)] LIKE TypeOf(VariableName) [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

For fixed-len strings use the following syntax:

DIM VarName[(subscripts)] AS {STRING | ASCIIZ]} * Size [ {VALUE | = | VALUE =} InitialValue ]

 

For absolute variables or pointers use the following syntax:

DIM VarName[(subscripts)] AS VarType [AT Address | PTR] ...

DIM VarName[(subscripts)], VarName[(subscripts)] AS VarType [AT Address | PTR] ...

 

Alternative Syntax 1: prefix data type

 

Alternatively, variables can be declared using prefix data type:

 

{Variable Type} [scope modifier] VarName[(subscripts)] [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

Where:

Variable Type can be any of the primary variable types supported by thinBasic:
BYTE, INTEGER, WORD, DWORD, LONG, QUAD, SINGLE, DOUBLE, EXT, CURRENCY, STRING, ASCIIZ, VARIANT, GUID, NUMBER

Scope modifier can be: LOCAL (default), GLOBAL, STATIC

 

All other options remain the same as standard DIM statement.

 

Alternative Syntax 2: predefined data type

 

Alternatively, variables can be declared using predefined data type:

 

DIM AS VariableType VarName[(subscripts)] [AT Address] [ {VALUE | = | VALUE =} InitialValue ]

 

Where:

VariableType can be any of the primary variable types supported by thinBasic:
BYTE, INTEGER, WORD, DWORD, LONG, QUAD, SINGLE, DOUBLE, EXT, CURRENCY, STRING, ASCIIZ, VARIANT, GUID, NUMBER

 

All other options remain the same as standard DIM statement.

 

Returns

 

None

 

Parameters

 

Remarks

 

DIM keyword can be substituted with LOCAL or GLOBAL or STATIC or CONST. depending on the context of where they are used.

 

VALUEThe Value option is used to initialize variable to a specific value.
It can be used for any numeric, string or variant variable.
In case of array, VALUE clause initialize all the array elements to the given value.

 

VarTypeThe following variable types can be specified:
BYTE, INTEGER, WORD, DWORD, LONG, QUAD, SINGLE, DOUBLE, EXT, CURRENCY
STRING, ASCIIZ
VARIANT
GUID
UDT (User Defined Types or data structures) name previously defined.

 

AT address | PTR
This notation is also called Absolute location or addressing.
When a variable is created thinBasic sets each element of a numeric variable/array to zero, and sets each element of regular string variable/arrays to a null string (length zero).  However, when an absolute location is specified (at a specific location in memory using the AT address syntax), thinBasic does not initialize the memory occupied by the variable/array.  Further, when an absolute variable/array is erased, the memory is not released either.  This provides a powerful mechanism to create overlay structures in memory.
 
The most common use of an absolute variable/array is when manipulating variable/arrays generated by external DLL. This involves obtaining a pointer to the variable/array, the element size, and the number of elements.  With this information, an absolute variable/array can be dimensioned in thinBasic and the variable/array memory manipulated directly.  Another common use involves using a large dynamic or fixed-length string memory block, overlaid with an absolute numeric array.
 
Care must be exercised when using absolute variable/arrays, since the contents of an absolute array can only be valid for the scope of the memory the array references.  If an absolute variable/array references memory that is LOCAL to the sub/function, the variable/array contents become invalidated if the target memory block is released.

 

LIKEThe Like option is used to initialize variable to a specific type whose indication is deducted by a string expression. Examples:
Dim MyVar_1 Like "String"
Dim MyVar_2 Like "Long"
Dim MyVar_3 Like "S" & "t" & "ring"

 
 

Restrictions

 

DIM ... without any AS clause is accepted as valid statement. In this case VARIANT data type is assumed.

So, the following is a valid statement:

Dim MyVar '---MyVar will be a VARIANT

 

In case of array dimensioning, if any of the indicated subscripts will be zero, the full array will not have any dimension and the array will be created as empty array. Use REDIM to assign new dimensions and allocate memory.

 

See also

 

ReDim

 

Examples

 

'---Some variables

Dim MyVar     As Number Value Timer

Dim MyStr     As String Value App_Path

Dim CountItems As Long   Value -1

 

Dim MyAsciiz As ASCIIZ * 256

Dim MyFixedLenString As STRING * 1024 VALUE "This is a big buffer string"

 

'---Other possible declaration ways

DIM a AS LONG

DIM a, b, c, d AS LONG

DIM a, b, c, d AS LONG = 123

DIM a AS LONG, b AS STRING, c AS EXT

DIM a AS LONG = 123456, b AS STRING = "ABC", c AS EXT = 1234.567

 

Long MyLong

String MyString

 

'---Arrays

Dim MaxItems As Long Value 10000

Dim MyArray(MaxItems) As String

Dim MyArray2(MaxItems) As String Value "Whatever string"

 

'---Undimensioned array redimensioned later

Dim MyUndimensionedArray() As String

'...

ReDim MyUndimensionedArray(MaxItems)

 

 

'---Examples using Dim ... Like ... Statement

Uses "console"

 

Dim i As Long

Dim MyLong Like "Long"

 

dim L like TypeOf(i)

string s

printl TypeOf(l)

printl TypeOf(s)

dim x like TypeOf(l)

printl TypeOf(x)