0% found this document useful (0 votes)
70 views4 pages

Lesson XVII:: Data Arrays and Control Arrays

This document discusses data arrays and control arrays in Visual Basic. It explains how to declare, initialize, manipulate, pass, and dynamically resize arrays. Control arrays allow grouping related controls, like textboxes, so they can be referred to and modified using an index. Event procedures for control arrays include the index of the control being used. The lesson concludes with an example application that asks the user to enter 10 numbers, stores them in an array, and calculates the average, demonstrating the use of data and control arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

Lesson XVII:: Data Arrays and Control Arrays

This document discusses data arrays and control arrays in Visual Basic. It explains how to declare, initialize, manipulate, pass, and dynamically resize arrays. Control arrays allow grouping related controls, like textboxes, so they can be referred to and modified using an index. Event procedures for control arrays include the index of the control being used. The lesson concludes with an example application that asks the user to enter 10 numbers, stores them in an array, and calculates the average, demonstrating the use of data and control arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson XVII:

Data Arrays and Control Arrays

Objectives

To
To
To
To
To

understand the array data structure


understand controls arrays
use control arrays in an application
be able to declare and manipulate arrays
be able to pass arrays to procedures

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:

Private Sub ComputeAverage( x() As Integer )


The header indicates that the procedure expects to receive integer array in formal parameter x.
The size of x is not specified because it will be referring to the actual argument array grade() (by
virtue of pass by reference).
On the other hand, if the procedure expects to receive an individual array element, you write the
procedure header as you do in expecting non-array arguments. An example is:
Private Sub ComputePercentage( x As Integer )
Variable x will be referring to element score(3).
IN FOCUS: MULTI-DIMENSIONAL ARRAYS
Arrays can have multiple dimensions. One use of this is when you want to represent data in a table
(where each data, is in a spreadsheet, is referred to by its column and row number). In Visual Basic,
we declare, say a 2-dimensional array, through:
Dim point(5,5) As Integer
You manipulate multi-dimensional arrays in the same manner as you do a one-dimensional array
(you can pass the array to procedures, use mathematical expressions as indices, etc).
Note: Refer to each element as point(2,3) and not as point(2)(3). The latter is a syntax error.
Visual Basic supports 60 array dimensions, but normally, programmers only need to use no more
than 3 dimensions.
IN FOCUS: DYNAMIC ARRAYS
Dynamic arrays are flexible arrays their size can be changed during the course of the program
execution. Dynamic arrays are very useful when you do not know how many elements to store in
the array during development (programming) time.
A dynamic array is not given a size when declared. For example:
Dim student() As String
You can resize the array at runtime through the statement:
ReDim student(8)
ReDim must be written within procedures. You can ReDim an array many times. It can also be
used to change the index bounds. The statement
Redim student(3 to 10)
changes the lower and upper bound to 3 and 6, respectively.
Note: During declaration, the number of dimensions of the array is not yet specified. This is
determined the first time ReDim is used. Once this is done, the number of dimensions cannot be
changed.
When ReDim is used, all the contents of the array are erased. Numeric arrays are initialized to 0
and Strings to a zero-length String. If you want to retain the values, use the keyword Preserve
as in the following statement:
ReDim Preserve student(8)
IN FOCUS: CONTROL ARRAYS
If you need to erase the text property of 10 TextBoxes, what do you do? You make 10 statements:
txtName0.Text = , txtName1.Text = , , txtName9.Text = . This is one heck of a job. With
control arrays, you can group related controls and refer to each control by an index. We then refer
to the TextBoxes as txtName(0), txtName(1), , txtName(9). To solve the problem, we write the
following code:
For x= 0 To 9
txtName(x).Text =

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.

Typically, an event procedure for a CommandButton is in the form:


Private Sub cmdSum_click ( )

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

Dim numbers(10), x As Integer


Private Sub cmdAdd_Click()
lblOutput.Caption = lblOutput.Caption & "[" + txtNum.Text & "]"

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.

Create the following application:


a. Using an Input Box, ask for how many student names are to be entered.
b. Ask the names of these students using a series of Input Boxes. Store the names in a
data array.
c. When the all the names have been inputted, load a Form.
d. The Form contains a TextBox and a CommandButton labeled Search. The user may
enter a name in the TextBox and presses the button to search this name. If the name is in
the array, display a Message Box with caption Name found!. Otherwise, display Name not
found!.

You might also like