Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
Declaring Array Variables
◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
Declaring Array Variables
■ An array is a group of objects referenced with the same variable name.
The individual values in an array are called elements. Array elements are
also variables, and can be used in any Visual Basic statement or function
that uses variables.
■ You set the dimensions of an array when you either:
• Use the array for the first time
• Declare the name, type, and number of elements in the array
See: ◄DIM Statement►
■ Each element in an array is referred to by an array variable name
subscripted with an integer or an integer expression:
MyArray(32)
Note: Noninteger numeric expressions used as array subscripts are rounded
to integer values.
■ An array variable name has as many subscripts as there are dimensions in
the array. For example:
This usage... Refers to a value in...
═════════════ ═══════════════════════════════════════════════
V(10) A one-dimensional array
T$(1,4) A two-dimensional string array
■ Use the DIM statement to set the following values of an array:
• Number of dimensions (maximum: 60)
• Upper subscript value (default: 10 for any array dimension)
Note: The maximum number of array elements per dimension is 32,767.
■ Use the OPTION BASE statement to set the lower boundary for array
subscripts. See: ◄OPTION BASE Statement►
■ You can have arrays of any simple variable type, including records. To
declare an array of records, you must first declare the data type in a
TYPE statement and then dimension the array. For example:
TYPE TreeNode
LeftPtr AS INTEGER
RightPtr AS INTEGER
DataField AS STRING * 20
END TYPE
DIM Tree(500) AS TreeNode
Each element of the array Tree is a record of type TreeNode. If the
array Tree is not part of a nested set of arrays of records, or if it is
the outermost array in a nested set of arrays of records, it may be
dynamic. For example:
TYPE TreeNode
LeftPtr AS INTEGER
RightPtr AS INTEGER
DataField AS STRING * 20
END TYPE
DIM Tree() AS TreeNode
■ To use a particular element of a record in an array, use the dot notation
form (variablename.elementname):
CONST MAXEMPLOYEES = 500
TYPE EmployeeRec
FullName AS STRING * 25
HireDate AS STRING * 9
END TYPE
DIM Employees(MAXEMPLOYEES) AS EmployeeRec
.
.
.
PRINT Employees(I).FullName;" ";Employees(I).HireDate
■ Array names are distinct from simple variable names. For example, the
array variable T and the simple variable T are two different variables:
DIM T(11)
T = 2 : T(0) = 1 'T is simple variable
FOR I% = 0 TO 10 'T(0) is element of array
T(I% + 1) = T * T(I%)
NEXT
■ Array elements, like simple variables, require a certain amount of memory,
depending on the variable type.
■ To find the total amount of memory required by an array, multiply the
number of elements by the bytes per element required for the array type.
For example:
DIM Array1(1 TO 100) AS INTEGER
DIM Array2(-5 TO 5)
• Array1 has 100 integer (2-byte) elements; 100 * 2 = 200, so its values
take up 200 bytes of memory.
• Array2 has 11 double-precision (8-byte) elements; 11 * 8 = 88, so its
values require 88 bytes of memory.
■ Because Visual Basic must store information about the array with the
array's values, arrays take slightly more memory than just the space for
the values.
See: ◄Variables Summary►