|
Arrays are variables with multiple values. They can be used for many purposes.
Each value is referred to by a number in ()s after the variable name for
the array and set based on that number. An array's elements can contain any
type of
data.
Creating an Array
Creating an array is simple and there are many different ways to do it. The following ways
can be used, but should not be combined:
- Dim
ArrayName (Elements)
Use this to setup an array with Element
number of values.
- Dim
ArrayName ()
Use this to set up an array with no given number of values, you
can add as many values as you want, but this is used in the case where you
set up may setup an
array that will change size. To change its size, just call:
ReDim [Preserve] ArrayName (Elements),
use preserve only when you want to keep the last array's values.
- Dim
ArrayName
Set ArrayName =
Array("ElementValue1", "ElementValue2")Use this to define
the values for the array's elements when it is created.
Using an Array
To set and retrieve the values in an array, all you need to do is give the array's name
with a number in ()s. This number is the value or element you want.
Examples:
'To set arrays
ArrayName(4) = "Nobody home"
ArrayName2(4) = 24
'To use their values
Name = "Homer " & ArrayName(2)
A = 33 * ArrayName2(3)
'Using arrays in functions
'Passing an element as a variable
B = SQR(NumArray(1))
'Passing an entire array to a function expecting an array.
UserID = 5
UserName = FindUser (UserNameArray, UnserIDArray, UserID)
When using an array as the parameter in a function, you must know how the array is
being used for that function. If you just need one of its elements as a parameter,
then you must include the element identifier, the number in ()s. This is
when you are using the element in place of a normal variable. When, you need an array
as the parameter and not a single element; then use its name without the number in the ()s.
By: Matthew Holder
|