Arrays
Arrays
1. Fixed arrays
2. Dynamic arrays
Fixed Arrays
Fixed arrays have a fixed size which cannot be changed at run-time.
Declaration of Arrays
Dim, Public,
and Private declare the array and its scope. Using Dim in a
procedure will make the array only available from within that
procedure. Using it in the General Declarations section will make it
available to all procedures in that module. Private has the same effect
and should be used only at the modular level. Using Public will make
the array available throughout the project.
ArrayName is the name of the array.
Subscript is the dimensions of the array.
DataType is any valid data type.
example
value=arrayname(index)
Eg.
x=a(2)
msgbox(x) - > 1
for i=0 to 4
print a(i)
next i
output:
Dynamic arrays differ from fixed arrays because a subscript range for
the array elements is not specified when the array is dimensioned.
Instead, the subscript range is set using the ReDim statement.
Syntax for ReDim statement –
Dim arrayname() as datatype
ReDim [Preserve] arrayname(subscripts)
Where,
The Preserve keyword helps to preserve the data in an existing array, when
you resize it.
arrayname is the name of the array to re-dimension.
subscripts specifies the new dimension.
Array dimensions
The above Visual Basic code creates a two dimensional String array of 3 rows
and 2 columns.