Introduction to arrays in Visual Basic 6

Tutorial Summary

This tutorial will introduce you using arrays in Visual Basic 6.

You will be shown what an array is, how to declare them, store values in them and retrieve data from them using the example array shown in the image below.

 

 

 

What is an array?

An example array called arrayFoods that stores strings. It has six slots.An array is rather like a variable - it's used for storing data in a box with a name.

The big difference is that unlike a variable (which can only store one item of data) an array can store more than one item of data.

Inside an array is a number of slots.

In each slot you can store one item of data.

To store data in a slot or to get data out of a slot you need to use the name of the array and the slot number.

In the example to the right there is an array called arrayFoods with 6 slots. Each slot stores a string.

Declaring (creating) an array

Declaring an array is easy. In fact it's almost the same as declaring a variable. To create the array shown here, I would use the following line of code:

How to declare an array

Where to declare an array

Local declaration

If you only want to use your array inside a sub-routine then declare the array where you'd declare the variables - at the top, just after the Private Sub line.

Private Sub Form_Load

' Declare a local array called arrayNames that is only accessible inside Sub Form_Load

Dim arrayNames(1 to 5) As String

End Sub

 

Remember, if you do this, the array will only exist inside that sub-routine - it will never be visible to other sub-routines!

 

Global declaration

If you want to use the array in more than one sub-routine then declare the array at the top of the code just under the Option Explicit line.

Option Explicit

' Declare a global array called arrayNames that can be accessed by all the sub-routines in the program

Dim arrayNames(1 to 5) As String

If you create a global array like this, you will need to use a sub-routine to put values into it.

You cannot put data into a global array outside of a sub-routine!

For global arrays, the ideal sub-routine to use is the Form_Load event as that is triggered every time the program starts but before the user can start to use the program.

Putting data into an array

Using my arrayFoods example from above, if I want to store an item in the third slot of the array (where "Fried Chicken" is currently stored) I would use the code on the right:

 

 

Displaying or using a value from the array

Getting data from one of the array slots is just as easy as using a variable. The only difference is that you need to supply a slot number too.

If I wanted to display the 4th slot of my food array in a message box I would use this code:

This would display "Fish & Chips" in a message box.

 

Example project

Create a new project in VB6.

Double-click on the form to create the Form_Load sub-routine and edit the code so that it looks like this:

' Variables must be explicitly declared before they can be used
Option Explicit

' Declare a GLOBAL array that has 6 slots to store strings in
Dim arrayFoods(1 To 6) As String

Private Sub Form_Load()
    ' The Form_Load event is triggered when the program starts

    ' Store an item of food in each slot of arrayFoods
    arrayFoods(1) = "Cheese On Toast"
    arrayFoods(2) = "Sirloin Steak"
    arrayFoods(3) = "Fried Chicken"
    arrayFoods(4) = "Fish & Chips"
    arrayFoods(5) = "Boiled Eggs"
    arrayFoods(6) = "Banana Bread"

    ' Show a message box that displays slot 4 ("Fish & Chips")

    MsgBox arrayFoods(4)
End Sub