An array is a consecutive group of memory locations that all have the same name
and the same type. To refer to a particular location or element in the array,
we specify the array name and the array element position number.
The Individual elements of an array are identified using an index. Arrays have
upper and lower bounds and the elements have to lie within those bounds. Each
index number in an array is allocated individual memory space and therefore users
must evade declaring arrays of larger size than required. We can declare an array
of any of the basic data types including variant, user-defined
types and object variables. The individual elements of an array are all of
the same data type.
Declaring arrays
Arrays occupy space in memory. The programmer specifies the array type and
the number of elements required by the array so that the compiler may reserve
the appropriate amount of memory. Arrays may be declared as Public (in a code
module), module or local. Module arrays are declared in the general declarations
using keyword Dim or Private. Local arrays are declared in a procedure using Dim
or Static. Array must be declared explicitly with keyword "As".
There are two types of arrays in Visual Basic namely:
Fixed-size array
: The size of array always remains the same-size doesn't change during
the program execution.
Dynamic array :
The size of the array can be changed at the run time- size changes during the
program execution.
Fixed-sized Arrays
When an upper bound is specified in the declaration, a Fixed-array is created.
The upper limit should always be within the range of long data type.
Declaring a fixed-array
Dim numbers(5) As Integer
In the above illustration, numbers is the name of the array, and the number
6 included in the parentheses is the upper limit of the array. The above declaration
creates an array with 6 elements, with index numbers running from 0 to 5.
If we want to specify the lower limit, then the parentheses should include
both the lower and upper limit along with the To keyword. An example for this
is given below.
Dim numbers (1 To 6 ) As Integer
In the above statement, an array of 10 elements is declared but with indexes
running from 1 to 6.
A public array can be declared using the keyword Public instead of Dim as shown
below.
Public numbers(5) As Integer
Multidimensional Arrays
Arrays can have multiple dimensions. A common use of multidimensional arrays
is to represent tables of values consisting of information arranged in rows and
columns. To identify a particular table element, we must specify two indexes:
The first (by convention) identifies the element's row and the second (by convention)
identifies the element's column.
Tables or arrays that require two indexes to identify a particular element
are called two dimensional arrays. Note that multidimensional arrays can have
more than two dimensions. Visual Basic supports at least 60 array dimensions,
but most people will need to use more than two or three dimensional-arrays.
The following statement declares a two-dimensional array 50 by 50 array within
a procedure.
Dim AvgMarks ( 50, 50)
It is also possible to define the lower limits for one or both the dimensions
as for fixed size arrays. An example for this is given here.
Dim Marks ( 101 To 200, 1 To 100)
An example for three dimensional-array with defined lower limits is given below.
Dim Details( 101 To 200, 1 To 100, 1 To 100)
Dynamic Arrays
There will be a situation when the user may not know the exact size of the
array at design time. Under such circumstances, a dynamic array can be initially
declared and can add elements when needed instead of declaring the size of the
array at design time.
Dynamic arrays are more flexible than fixed-arrays, because they can be resized
anytime to accommodate new data. Like fixed-sized arrays, dynamic arrays have
either Public (in code modules), module or local scope. Module dynamic arrays
are declared using keyword Dim or Private. Local dynamic arrays are declared with
either Dim or Static.
e.g.: Dim dynArray ( )
The actual number of elements can be allocated using a ReDim statement. This
example allocates the number of elements in the array based on the value of the
variable, x.
ReDim dynArray ( x + 1 )
The Redim Statement can appear only in a procedure, which is an executable
statement. The same way of declaration as used for fixed arrays is used for delaring
Redim statements also. Redim is an executable statement. The lower an upper limits
for each dimension caln also be specified explicitly as in a fixed size array.
An example for this is given below.
Redim FirstArray (4 to 12)
Each time on executing the ReDim statement, the current data stored in the
array is lost and the default value is set. But if we want to change the size
of the array without losing the previous data, we have to use the Preserve keyword
with the ReDim statement. This is shown in the example given below.
ReDim Preserve dynArray ( 50 To 200)
When the Preserve keyword is used, only the upper limit of the last dimension
in a multidimensional array can be changed. No other dimensions or the lower limit
of the last dimension can be changed.