Initializing Arrays
VB includes a special built-in function called Array().
The Array() function lets
you declare and initialize an array quickly. Suppose
you want to store the number of days in each month
(ignoring leap year) in an array named Days.
You can declare it and initialize as:
Dim Days As Variant
Days = Arrays(31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31)
The Array function returns a Variant containing an
array. If the Option Base 1 statement appears in the
module's Declarations section, the first subscript
of Array() is 1, or 0 otherwise.
Using arrays
In the previous section, you have seen that arrays
can be used in calculations just as non-array variables.
The following program segment shows two arrays for
storing the name and the age of 5 persons.
Private Sub storeValue()
Dim strName(5) As String
Dim intAge(5) As Integer
Dim intCtr As Integer
For intCtr = 0 To 4
strName(intCtr)
= InputBox ("Enter the next name")
intAge(intCtr)
= InputBox ("Enter the age")
Next intCtr
intCtr = 0
Do
Msgbox "Name
" & intCtr & " is " & strName(intCtr)
Msgbox "Age
" & intCtr & " is " & intAge(intCtr)
intCtr = intCtr
+ 1
Loop Until (intCtr > 4)
End Sub
|
|
In this chapter, you were introduced
to Arrays. An array is a group of elements that
share a common name, and that are differentiated
from one another by their positions within the
array.
|
|