DIM
 
Syntax


Declaring Integers:   
    DIM  symbolname [AS INTEGER]        ' INTEGER type is assumed if no type is specified

Declaring Floating point (requires firmware 7.50 or 8.11)
    DIM symbolname, symbolname2, symbolname3 AS SINGLE

Declaring Arrays:
    DIM symbolname (max_element )
    DIM symbolname (max_element ) AS BYTE

Declaring Strings:
    DIM  symbolname$ (max_element )
    DIM  symbolname (max_element ) AS STRING 

Description


Declares a named variable and allocates memory to accommodate it. Though ARMbasic does not require the declaration of integer variables, DIM is used to declare floating-point singles or arrays, arrays of integers, or strings (arrays of bytes).  The size is the max_element in the array plus 1.  This allows indexing from 0 to max_element

For backward compatibility strings may have the last character the dollar sign $.  Multiple variables of the same type may be defined on a single DIM statement, that includes both individual variables and arrays of the same type.

Memory for simple variables is allocated from the start of a heap, and strings or arrays are allocated from the top or end of the heap.  Strings are packed as bytes and always word aligned, you must allow enough space to accommodate the expected maximum size of the string plus 1 byte for a termination (0) character.  String operators rely on the terminator. Strings may not exceed 255 characters.

Simple INTEGER variables will be automatically declared on first use, unless you use DIM  symbolname AS INTEGER.  At which point all subsequent integers must be declared using a DIM.  Classic BASIC declared all variables automatically, but VB requires them all to be declared with a DIM statement.  The advantage in VB is that if you mistype a variable name it will be flagged as undefined rather than becoming a newly automatically declared INTEGER variable.  As the authors (read me) came from classic BASIC, the automatic declaration is default, but if you don't like that just use a DIM to declare an INTEGER variable early in the program.  That statement should be after the #includes as many libraries use auto-declaration.

Floating point SINGLE use a single word of storage and are allocated from the start of the heap.

SUB and FUNCTION procedures also use DIM between SUB/FUNCTION .. ENDSUB/ENDFUNCTION. Those variables will be local to the procedure.  Using DIM here does not change whether all subsequent integers must be declared using a DIM or not.  In other words the state whether DIM is required is saved upon entering a SUB procedure and is restored at the ENDSUB.

Arrays of 8bit values can be declared AS BYTE.  BYTE arrays are not limited to 255 characters.  However, string operations and functions ARE still limited to 255 bytes.

Example

DIM a$ (10)
DIM b (20)  AS STRING
DIM c (300)  AS BYTE


a$  = "Hello World"
b  = "... from ARMbasic!"
c = a$ + b     ' you can mix STRING and BYTE arrays, but the results are limited to 255 characters and are truncated at the first 0

print c                   ' displays Hello World... from ARMbasic



Differences from other BASICs

 

See also