Lesson XVII:: Data Arrays and Control Arrays
Lesson XVII:: Data Arrays and Control Arrays
Objectives
To
To
To
To
To
Notes
IN FOCUS: DATA ARRAYS
Arrays are data structures consisting of contiguous memory cells of the same name and type. For
example, we can have an array of student names. In Visual Basic, we refer to each element in an
array through an index e.g. name(0), name(1), .., name(n).
Declaration
Arrays should be declared so that the compiler can determine the amount of memory space to
allocate. Recall that data types have different memory requirements. You declare arrays through
the following syntax:
Dim <var_name> As <data_type>
Examples:
a. Dim grade(7) As Integer
b. Dim grade(7) As Integer, name(7) As String
Example a. tells the compiler to reserve six elements for array grade of type Integer. The value 7
tells defines the upper bound of the array. The compiler allocates 7 cells of equal memory sizes
with indices starting at 0 (the lower bound) until 6.
Note: Unlike other variables, arrays should implicitly be declared. Not doing so is a syntax error.
Arrays may be declared in Code or Form Module but may only be declared Public in the former.
Numeric array elements are initialized to zero (0) by default. Strings are initialized to a zero-length
String. The programmer can also initialize the array with user-defined values. Take the following
examples:
a. grade(0) = 9
grade(1) = 5
b. For x = 0 To 7
grade(x) = 2
Next x
c. For x = 0 To 6
grade(x+a) = 2 Make sure that the expression x+a evaluates to a Long value
Next x
Passing Arrays
Arrays can be passed to a procedure. To do so, specify the name of the array followed by a pair of
empty parentheses. For example:
Call ComputeAverage( grade() )
You can also pass selected elements of an array, as in the following example:
Call ComputePercentage( score(3) )
Take note that array or individual array element arguments are always passed by reference the
procedure can modify the contents of the original array.
On the part of the called procedure, its parameter list must specify that an array will be received.
The procedure header of ComputeAverage might be written as:
Next x
Note that the names of the controls are all the same. They only vary in index (when a control is
part of a control array, an Index property is added). A control array is an array of the same control.
This means you cannot have an array composed of TextBoxes and Labels. One good thing about
controls array is that when you format one control (e.g. changing the BackColor property), the
format is applied to all other controls in the array.
So how do you create a control array? When you name a control with an existing name (of the
same type of control), Visual Basic displays a dialog box asking you if you want to make a control
array. Just click Yes. If you click the No button, VB renames the control. The dialog box appears
below.
End Sub
If we have a control array, we have controls with the same name. How do we know then which
button has been pressed? All Event Procedures related to a control array has a special argument
value passed to them. This value determines which control is being worked on. Below is a click
event of a CommandButton that is part of a control array.
Private Sub cmdSum_click (Index as Integer)
End Sub
How do you modify the properties of control array at runtime? Refer to a specific member of the
control array using an index. For example,
txtGrade(0).Text = 90
lblResult(4).Caption = Computer
Lesson in Action
Lets create an application that asks for a maximum of 10 numbers and displays the average of
these numbers.
txtNum
cmdClear
cmdAdd
lblOutput
cmdCompute
lblAverage
numbers(x) = Val(txtNum.Text)
x=x+1
If x = 10 Then
cmdAdd.Enabled = False
End If
End Sub
Private Sub cmdClear_Click()
x=0
lblOutput.Caption = " "
End Sub
Private Sub cmdCompute_Click()
Sum = 0
For y = 0 To x - 1
Sum = Sum + numbers(y)
Next y
lblAverage.Caption = " Average: " & Format((Sum / x), "0.0")
End Sub
Private Sub Form_Load()
x=0
End Sub
On your Own
1. Create an application that asks for 10 numbers (to be entered in a control array of 10
TextBoxes) and outputs the average of these 10 numbers in a Label. Create a function that
receives an array of these values to do the averaging.
2.