Navigation:  thinBasic language > Data types and variables >

DIM

Previous pageReturn to chapter overviewNext page

 

Description

 

Declare and dimension variable and/or arrays.

 

Syntax

 

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 ]

 

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] ...

 

 

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

 

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

 

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

 

 

'---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)

 

 

 

© 2004-2008 thinBasic. All rights reserved. Version 1.7.0.0 Web Site: http://www.thinbasic.com