* *
*
* * News
 • Daily news
 • Archived news

Columns
 • WildCard
 • Qbasic Articles
 • QB Comic!

Learning center
 • QB Books
 • Qbasic Lessons!
 • Qbasic FAQ
 • Newbies Section
 • Qbasic Tutorials

* *
*
*
*
*  
.DIM Statement Details.

  QuickSCREEN      Details      Example      Contents      Index
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
DIM Statement Details
Syntax
  DIM [SHARED] variable[(subscripts)][AS type]
               [,variable[(subscripts)][AS type]...
 
  Argument     Description
  SHARED       The optional SHARED attribute allows all procedures
               in a module to share arrays and simple variables. This
               differs from the SHARED statement, which affects only
               variables within a single SUB or FUNCTION.
 
  variable     A BASIC variable name.
 
  subscripts   The dimensions of the array. Multiple dimensions can be
               declared. The subscript syntax is described below.
 
  AS type      Declares variable to be an elementary or user-defined
               type. The elementary types are INTEGER, LONG, SINGLE,
               DOUBLE, and STRING (variable or fixed).
 
 
Subscripts in DIM statements have the following form:
 
  [lower TO] upper [,[lower TO] upper]...
 
The TO keyword provides a way to indicate both the lower and the upper
bounds of an array's subscripts. The following statements are
equivalent (if there is no OPTION BASE statement):
 
  DIM A(8,3)
  DIM A(0 TO 8, 0 TO 3)
  DIM A(8,0 TO 3)
 
With the TO keyword, you are no longer restricted to positive
subscripts. You can use TO to specify any range of subscripts
from -32,768 to 32,767:
 
  DIM A(-4 TO 10)
  DIM B(-99 TO -5,-3 TO 0)
 
If you use an array in your program without including the array in a
DIM statement, the maximum value of each subscript of the array is 10.
If you use a subscript that is greater than the specified maximum, an
error message appears that says "Subscript out of range."
 
The DIM statement initializes all elements of numeric arrays to zero
and all the elements of string arrays to null strings. The fields of
record variables are initialized to zero, including fixed-string
fields. The maximum number of dimensions allowed in a DIM statement
is 60.
 
If you try to dimension an array variable with a DIM statement
after you have referred to the array, an error message results that
reads "Array already dimensioned." It is good programming
practice to put the required DIM statements at the beginning
of a program, outside of any loops.
 
Static and Dynamic Arrays
 
How you declare an array also determines whether it is $STATIC
(allocated when the program is translated) or $DYNAMIC (allocated
when the program is run).
 
  ž An array declared first in a COMMON statement is $DYNAMIC.
  ž Implicitly dimensioned arrays are $STATIC.
  ž Arrays dimensioned with numeric constants or CONST statement
    constants are $STATIC.
  ž Arrays dimensioned with variables as subscripts are $DYNAMIC.
 
The following list shows the different combinations and results:
 
  Statement       Result
 
  DIM A(0 TO 9)   The array A is allocated as a $STATIC array if
                  is $DYNAMIC not in effect.
 
  DIM A(MAXDIM)   If MAXDIM is defined in a CONST statement A is a
                  $STATIC array. If MAXDIM is a variable, then the
                  array is a $DYNAMIC array and is only allocated
                  when the program reaches the DIM statement.
 
  Note: If the array size exceeds 64K, if the array is not dynamic,
        and if the /AH option was not used, you may get an error message
        that reads "Subscript out of range" or one that reads "Array
        too big." Reduce the size of the array or make the array dynamic
        and use the /AH command-line option.
 
Type Declarations
 
In addition to declaring the dimensions of an array, the DIM
statement may also be used to declare the type of a variable. For
example, the following statement declares the variable to be an
integer, even though there is no type declaration character or DEFINT
statement:
 
  DIM NumberOfBytes AS INTEGER
 
The DIM statement provides a mechanism for declaring specific
variables to be records. In the following example, the variable
TopCard is declared as a record variable:
 
  TYPE Card
     Suit AS STRING * 9
     Value AS INTEGER
  END TYPE
 
  DIM TopCard AS Card
 
You may also declare arrays of records:
 
  TYPE Card
     Suit AS STRING * 9
     Value AS INTEGER
  END TYPE
 
  DIM Deck(1 TO 52) AS Card
 
Differences from BASICA
 
BASICA executes a DIM statement when it encounters the statement in
the program. The array is only allocated when the statement is
executed, so all arrays in BASICA are dynamic.
* * ** * * * *