Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 11 Using Arrays to Manage Numeric and String Data 281


Your form looks like the one shown in the following screen shot:


  1. In Solution Explorer, click the View Code button to display the Code Editor.

  2. Scroll to the top of the form’s program code, and directly below the Public Class Form1
    statement, type the following array declaration:


Dim Temperatures(0 To 6) As Single
This statement creates an array named Temperatures (of the type Single) that contains
seven elements numbered 0 through 6. Because the array has been declared at the top
of the form, it is available in all the event procedures in the form.


  1. Display the form again, and then double-click the Enter Temps button (Button1).


The Button1_Click event procedure appears in the Code Editor.


  1. Type the following program statements to prompt the user for temperatures and to
    load the input into the array:


Dim Prompt, Title As String
Dim i As Short
Prompt = "Enter the day's high temperature."
For i = 0 To UBound(Temperatures)
Title = "Day " & (i + 1)
Temperatures(i) = InputBox(Prompt, Title)
Next
The For... Next loop uses the short integer counter variable i as an array index to load
temperatures into array elements 0 through 6. Rather than using the simplified For
loop syntax:

For i = 0 to 6
Free download pdf