Visual Basic 6.0 Guide
Arrays

What is An Array?

An array is a data structure that allows you to store as many items as you need to using a single variable name. So far you have had to use a different variable name to store each different piece of information. For example,

Dim intFirstNumber As Integer
Dim intSecondtNumber As Integer
Dim intThirdtNumber As Integer

With an array we could store the numbers as follows.

Dim intNumbers(2) As Integer

The first number would be stored as intNumbers(0), the second at intNumbers(1) and so on. The number in brackets is called the subscript.

You have already used a control that works like this. What is it?

Example - Enter 5 Numbers

The code below asks the user to enter 5 numbers. These are stored in an array. The numbers are then printed on the form with the total and average.

Notice the way that for ..next loops have been used to fill and process the data in the array. Think about the lines of code that you have been saved from typing if we had used a different name for each of the numbers.

Private Sub Form_Load()
Form1.Show
Dim intNumbers(4) As Integer
Dim intCounter As Integer
For intCounter = 0 To 4
intNumbers(intCounter) = InputBox("Enter Number Stored At Index: " & intCounter)
Next intCounter
For intCounter = 0 To 4
Print intNumbers(intCounter)
Next intCounter
Dim intTotal As Integer
For intCounter = 0 To 4
intTotal = intTotal + intNumbers(intCounter)
Next intCounter
Print "Total: " & intTotal
Dim sglMean As Single
sglMean = intTotal / 5
Print "Mean: " & sglMean
End Sub

Control Arrays

A Control array is a group of controls (eg text boxes, labels, option buttons) all of the same type. The advantage of using a control array is that all of the controls share the same name and we can use loops to process them.

Example We are going to create a program that uses an array of text boxes and an array of labels. The user enters numbers into the text boxes and we will put them into an array like the previous example. We will then fill the labels with the values stored in our new array.

Step 1

Open a new project and add a text box to the form. Call the text box txtNumbers.

Step 2

Click on the textbox and copy it. Click on Edit, Paste and when asked if you want to create a control array, say Yes.

Step 3

Keep pasting text boxes onto the form until you have 5. Check the names of the text boxes - they should be called txtNumbers(0), txtNumbers(1), txtNumbers(2) etc and should be placed in order, one above the other.

Step 4

Repeat this process with a series of labels called lblNumbers.

Step 5

Add a command button to the form named cmdDisplay.

Your form should now look like the screenshot below.

form design

Step 6

Now we need to program the button to do something with the numbers that have been entered. Double click on the button and add the following code. The lines that begin with an apostrophe are comments - these can be added by programmers to remind them of what their code does. They are ignored by Visual Basic.

Private Sub cmdDisplay_Click()
Dim intNumbers(4) As Integer
Dim blnFilled As Boolean
blnFilled = True
Dim intCounter As Integer
'This loop checks to see if all of the text boxes have been filled
'If one of the boxes is empty then blnFilled is set to false
For intCounter = 0 To 4
If txtNumbers(intCounter).Text = "" Then
blnFilled = False
End If
Next
'Exit this procedure if blnFilled is false
If blnFilled = False Then
MsgBox "You Must Fill In All Of The Numbers"
Exit Sub
End If
'Fill the array with variables
For intCounter = 0 To 4
intNumbers(intCounter) = txtNumbers(intCounter).Text
Next
'Fill the labels with the contents of the array
For intCounter = 0 To 4
lblNumbers(intCounter).Caption = intNumbers(intCounter)
Next
End Sub

This code is not very efficient. You can see that there is a lot of repetition. Try to rewrite the code so that the same tasks are performed using fewer lines of code.