19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

num(5) = 10<br />

picOutput.Print Triple(num(5))<br />

End Sub<br />

Private Function Triple(x As Integer) As Integer<br />

Triple = 3 * x<br />

End Function<br />

When the program is run <strong>and</strong> the comm<strong>and</strong> button clicked, 30 will be displayed.<br />

3. <strong>Visual</strong> <strong>Basic</strong> provides two functions that simplify working with arrays that have<br />

been passed to a procedure. If an array has been dimensioned with the range m<br />

To n, then the values of the functions LBound(arrayName) <strong>and</strong> UBound(array-<br />

Name) are m <strong>and</strong> n, respectively.<br />

Private Sub cmdButton_Click()<br />

Dim chiefJustice(13 To 15) As String<br />

chiefJustice(13) = “Warren”<br />

chiefJustice(14) = “Burger”<br />

chiefJustice(15) = “Rehnquist”<br />

Call Display(pres())<br />

End Sub<br />

Private Sub Display(a() As String)<br />

Dim i As Integer<br />

For i = LBound(a) To UBound(a)<br />

picOutput.Print a(i) & “ ”;<br />

Next i<br />

End Sub<br />

When the program is run <strong>and</strong> the comm<strong>and</strong> button clicked, “Warren Burger<br />

Rehnquist” will be displayed.<br />

6.3 CONTROL ARRAYS<br />

We have seen many examples of the usefulness of subscripted variables. They are essential for<br />

writing concise solutions to many programming problems. Because of the great utility that<br />

subscripts provide, <strong>Visual</strong> <strong>Basic</strong> also provides a means of constructing arrays of text boxes, labels,<br />

comm<strong>and</strong> buttons, <strong>and</strong> so on. Because text boxes, labels, <strong>and</strong> comm<strong>and</strong> buttons are referred<br />

to generically in <strong>Visual</strong> <strong>Basic</strong> as controls, arrays of these objects are called control<br />

arrays.<br />

Unlike variable arrays, which can only be created by Dim <strong>and</strong> ReDim statements once a<br />

program is running, at least one element of a control array must be created when the form is<br />

designed. The remaining elements can be created either during form design, or, perhaps more<br />

typically, with the Load statement when the program is run.<br />

To create the first element of an array of text boxes, create an ordinary text box, then<br />

access the Properties window, <strong>and</strong> select the property called Index. By default this property<br />

is blank. Change the Index property to 0 (zero). Your text box is now the first element in a<br />

subscripted control array. If the name of a text box is txtBox <strong>and</strong> its Index property is 0, then<br />

assigning a value to the text box during run time requires a statement of the form<br />

txtBox(0).Text = value<br />

Arrays are not of much use if they contain only a single element. To create additional<br />

elements of the txtBox( ) control array during form design, make sure that the element you<br />

just created is active by clicking on it. Next, press Ctrl+C (or open the Edit menu <strong>and</strong> select<br />

Copy). <strong>Visual</strong> <strong>Basic</strong> has now recorded all the properties associated with txtBox(0) <strong>and</strong> is<br />

Control Arrays 179

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!