| An array is a collection of similar
variables in which each has the same
name and they all have they same type
. The first element in an array usually
has an index value of 0 . An array
can be different sizes.
Here is the syntax of an array.
Dim | Public | Private ArrayName
(subscript ) As DataType
Dim | Public | Private are the keywords
which declare an array and its scope
. If you use Dim the array i sprivate
to the procedure in which its declared
. Public makes the array visible anywhere
in the program and Private makes the
array visible only to the form or
module in which its declared . I usually
declare an array in the General Declarations
section with Dim and this means the
array is available to all procedures
within the module.
ArrayName is the name that you use
for the array.
( subscript ) is the highest number
of the array.
As is the Visual Basic keyword that
signifies a type declaration
DataType is a valid Visual Basic
type , such as Integer
If you set Option Base 1 in the General
declarations section of a module then
the first element of the array would
be 1 but by default the first element
of an array is 0.
Example
Dim intMyArray(2) As Integer
intMyArray(0) = 24
intMyArray(1) = 58
intMyArray(2) = 7
This declares an array of 3 elements
of type integer and then assigns a
value to them.
You can also declare an array using
the To keyword , this is a convenient
way of setting your starting element
at a number other than 0 like this.
Dim intMyArray (1 To 10) As Integer
Changing the number of elements
To change the number of elements
in an array you have to redimension
it using the ReDim keyword , here
is the syntax.
ReDim [Preserve] ArrayName( subscript
) As dataType
ReDim is the keyword for redimensioning
Preserve is optional and this forces
all existing elements in an array
to hold their values . If this is
omitted all values in the elements
is lost and each element is changed
to zero for numerics and ""
(zero length string) for strings.
ArrayName is the name of the array
subscript is the highest value of
the array
As is the keyword that signifies a
type declaration
datatype is the valid Visual Basic
Type , Integer or Single for instance
Here is an example that redimensions
our first array
ReDim Preserve intMyArray(6) As Integer
|