Each time we read or input data into a variable the previous value read or
inputted is destroyed.
A variable can only hold one value at a time.
If we need to retain access to several previously inputted values we could
read/input them into separate variables
Dim strName1
As String
Dim strName2 As String
Dim strName3 As String
To use each of these variables we would need unique code for each unique
variable
strName1 = txtName.Text
MsgBox(strName1)
strName2 = txtName.Text
MsgBox(strName2)
strName3 = txtName.Text
MsgBox(strName3)
This might work if we only had 3 names but what about 25 or 50?
When we need to store several occurrences of the same type of data we use Variable Arrays
A variable array is a group of variables of the same data type with the same
name. You can use the same instructions to manipulate all the variables in the
array by varying an index to the array.
Whenever we refer to a variable array we must qualify it with an Index
MsgBox(strName(0))
MsgBox(strName(1))
If we use constants for the index we are no better off than when we used
different variable names.
We usually use an integer variable for an index
MsgBox(strName(intIndex))
The index can be any whole number. It can be in the form of a numeric
constant, a variable (Short, Integer or Long), or an expression that resolves to
a whole number.
The value of the index indicates which strName we want to access.
We must declare arrays before any reference can be made to it in code.
To define a fixed length array
Dim variablename(n to n) As datatype
Or
Dim variablename(n) As datatype
For Example
Dim strPhoneNum(1
To 50) As String
Dim strPhoneNum(0 To 49)
As String
Dim strPhoneNum(49) As String
Each of the previous Dim statements reserves memory for 50 string variables
called strPhoneNum.
When you use the strPhoneNum(n To n) form of the declaration you are identifying
the minimum and maximum index value that
will be used to access the array.
The standard way to declare arrays is.
Dim strPhoneNum(49) As String
Where then number indicates the maximum index value.
When working with computers we often start counting with zero.
The first element in the preceding array is referred to as
strPhoneNum(0)
The second element is strPhoneNum(1)
The fiftieth is strPhoneNum(49)
The index indicates a displacement from the beginning of the arrays.
IMPORTANT
You are responsible for staying within the bounds of your array. If your
index becomes too large (greater than the maximum allowed value) or too
small (less than 0) you will get an error message
You must always keep track of your index.
There are four basic things we do with arrays
Initialize or load values into an array from using constants, user input or from a file.
Dump the contents of the array for viewing (hard or soft copy) or to a file for permanent storage.
Lookup or search an array for a particular element.
Change an array element
We can also Sort an array, Insert elements into an array, and delete elements from an array.
To load values into an array from an InputBox( )
Dim strNames(24) As String
Dim intIndex As Integer
intIndex = 0
strNames(intIndex) = InputBox("Enter a name, 'quit' to
quit")
Do Until LCase(strNames(intIndex)) =
"quit"
intIndex += 1
strNames(intIndex) = InputBox("enter
a name, 'quit' to quit")
Loop
MsgBox("names in array " & intIndex)
To dump ALL the elements in the strNames array we can use a For Next loop
intIndex = 0
For intIndex =
0
to
24
MsgBox(strNames(intIndex))
Next
OR a For Each loop
Dim strName As String
For Each strName
In strNames
MsgBox(strName)
Next
To dump only the values stored in the array
intIndex = 0
Do Until LCase(strNames(intIndex)) =
"quit"
MsgBox(strNames(intIndex))
intIndex = intIndex + 1
Loop