By definition, an array is a variable with a single name that represents many different items. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item
For example, if we need to enter one hundred names, it is difficult to declare 100 different names. Besides, if we want to process those data that involves decision making, we might have to use hundreds of if...then statements, this is a waste of time and efforts.So, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item, for example, name(1), name(2), name(3) .......etc. , makes declaring variables more streamline.
An array can be one-dimensional or multidimensional. A one-dimensional array is like a list of items or a table that consists of one row of items or one column of items.
A two-dimensional array is a table of items that make up of rows and columns. The format for a one-dimensional array is ArrayName(x), the format for a two dimensional array is ArrayName(x,y) and a three-dimensional array is ArrayName(x,y,z) . Normally it is sufficient to use a one-dimensional and two-dimensional array, you only need to use higher dimensional arrays if you need to deal with more complex problems. Let me illustrate the arrays with tables
We can use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declares an array that could be used only in a local procedure.
The general syntax to declare a one dimensional array is as follow:
Dim arrayName(subscript) as dataType
where subs indicates the last subscript in the array.
When you declare an array, you need to be aware of the number of elements created by the Dim keyword. In the Dim arrayName(subscript) statement, subscript actually is a constant that defines the maximum number of elements allowed. More importantly, subs start with 0 instead of 1. Therefore, the Dim arrangeName(10) statement creates 11 elements numbered 0 to 11. There are two ways to overcome this problem, the first way is by uisng the keyword Option Base 1, as shown in Example 16.1.
Option Base 1 Dim CusName(10) as String
will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CusName(1) to CusName(10). Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)
The second way is to specify the lower bound and the upper bound of the subscript using To keyword. The syntax is
Dim arrayName(lowerbound To upperbound) As dataType
Dim Count(100 to 500) as Integer
declares an array that consists of the first element starting from Count(100)
and ends at Count(500)
Dim studentName(1 to 10) As String Dim num As Integer Private Sub addName() For num = 1 To 10 studentName(num) = InputBox("Enter the student name","Enter Name", "", 1500, 4500) If studentName(num)<>"" Then Form1.Print studentName(num) Else End End If Next End Sub
**The program accepts data entry through an input box and displays the entries in the form itself.
Dim studentName(1 to 10) As String Dim num As Integer Private Sub addName( ) For num = 1 To 10 studentName(num) = InputBox("Enter the student name") List1.AddItem studentName(num) Next End Sub Private Sub Start_Click() addName End Sub
**The program accepts data entries through an InputBox and displays the items in a list box.
The general syntax to declare a two dimensional array is as follow:
Dim ArrayName(Sub1,Sub2) as dataType
If you wish to compute a summary of students involve in games according to different year in a high school, you need to declare a two dimensional array. In this example, let's say we have 4 games, football, basketball, tennis and hockey and the classes are from year 7 to year 12. We can create an array as follows:
Dim StuGames(1 to 4,7 to 12 ) As Integer
will create an array of four rows and six columns, as shown in the following table:
In this example, we want to summarize the first half-yearly sales volume for four products. Therefore, we declare a two dimension array as follows:
Dim saleVol(1 To 4, 1 To 6) As Integer
Besides that, we want to display the output in a table form. Therefore, we use a list box. We named the list box listVolume. AddItem is a listbox method to populate the listbox.
Private Sub cmdAdd_Click() Dim prod, mth As Integer ' prod is product and mth is month Dim saleVol(1 To 4, 1 To 6) As Integer Const j = 1 listVolume.AddItem vbTab & "January" & vbTab & "February" & vbTab & "March" _ & vbTab & "Apr" & vbTab & "May" & vbTab & "June" listVolume.AddItem vbTab & "____________________________________________" For prod = 1 To 4 For mth = 1 To 6 saleVol(prod, mth) = InputBox("Enter the sale volume for" & " " & "product" & " " & prod & " " & "month" & " " & mth) Next mth Next prod For i = 1 To 4 listVolume.AddItem "Product" & "" & i & vbTab & saleVol(i, j) & vbTab & saleVol(i, j + 1) & vbTab & saleVol(i, j + 2) _ & vbTab & saleVol(i, j + 3) & vbTab & saleVol(i, j + 4) & vbTab & saleVol(i, j + 5) Next i End Sub *Note: the keyword vbTab is to create a space
The output is shown in the figure below:
So far we have learned how to define the number of elements in an array during design time. This type of array is known as static array. However, the problem is sometimes we might not know how many data items we need to store during run time. In this case, we need to use dynamic array where the number of elements will be decided during run time. In VB6, the dynamic array can be resized when the program is executing. The first step in declaring a dynamic array is by using the Dim statement without specifying the dimenson list, as follows:
Then at run time we can specify the actual array size using the ReDim statement,as follows:
ReDim myArray(1 to n) when n is decided during run time
You can also declare a two dimensional array using ReDim statement, as follows:
ReDim myArray(1 to n, 1 to m) when m and n are known during run time
In this example, we want to display the elements of an array in a list box. The size of the array will only be known during run time. It demonstrates the creation of a dynamic array using the ReDim keyword.
Private Sub cmd_display_Click() Dim myArray() As Integer Dim i, n As Integer n = InputBox("Enter the upper bound of array") List1.Clear For i = 1 To n ReDim myArray(i) myArray(i) = i ^ 2 List1.AddItem myArray(i) Next End Sub
Another problem related to ReDim is each time you ReDim an array, the values currently stored in the array are lost. In order to preserve the stored values, we can use the keywords ReDim Preserve, as follows
ReDim Preserve MyArray(n)
where n is the new upper bound of the array