 # Lesson 16 : Arrays

### 16.1 Introduction to Arrays

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.

### 16.2 Dimension of an Array

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

#### Table 16.1. One dimensional Array

 Student Name Name(1) Name(2) Name(3) Name(4)

#### Table 16.2 Two Dimensional Array

 Name(1,1) Name(1,2) Name(1,3) Name(1,4) Name(2,1) Name(2,2) Name(2,3) Name(2,4) Name(3,1) Name(3,2) Name(3,3) Name(3,4)

## 16.2 Declaring Array

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.

### 16.2.1 Declaring one dimensional Array

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.

### 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)

 CusName(1) CusName(2) CusName(3) CusName(4) CusName(5) CusName(6) CusName(7) CusName(8) CusName(9) 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

#### Example 16.2

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)

### Example 16.3

```Dim studentName(1 to 10) As String
Dim num As Integer
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.

#### Example 16.4

```Dim studentName(1 to 10) As String
Dim num As Integer
For num = 1 To 10
studentName(num) = InputBox("Enter the student name")
Next
End Sub
Private Sub Start_Click()
End Sub```

**The program accepts data entries through an InputBox and displays the items in a list box.

#### 16.2.2 Declaring two dimensional Array

The general syntax to declare a two dimensional array is as follow:

Dim ArrayName(Sub1,Sub2) as dataType

#### Example 16.5

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:

Year 7 8 9 10 11 12
Football StuGames(1,7) StuGames(1,8) StuGames(1,9) StuGames(1,10) StuGames(1,11) StuGames(1,12)
Basketball StuGames(2,7) StuGames(2,8) StuGames(2,9) StuGames(2,10) StuGames(2,11) StuGames(2,12)
Tennis StuGames(3,7) StuGames(3,8) StuGames(3,9) StuGames(3,10) StuGames(3,11) StuGames(3,12)
Hockey StuGames(4,7) StuGames(4,8) StuGames(4,9) StuGames(4,10) StuGames(4,11) StuGames(4,12)

#### Example 16.6

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.

#### The code

```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"
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: ### 16.3 Dynamic Array

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:

`      Dim myArray()`

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`

#### Example 16.7

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.

#### The Code

```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

Next

End Sub```

#### The output #### Figure 16.2

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