280 Part II Programming Fundamentals
“Using Loops and Timers .” The loop counter is used to reference each element in the
array. The array contents are then displayed on the form by using a For... Next loop
and a text box object. The average high temperature is also calculated and displayed—
how fun!
The UBound and LBound Functions
To simplify working with the array, the Fixed Array program uses the UBound function
to check for the upper bound, or top index value, of the array. With UBound, you can
process arrays without referring to the declaration statements that defined exactly how
many values the array would hold. The closely related LBound function, which confirms
the lower index value, or lower bound, of an array, is also available to you as a feature of
early versions of Visual Basic. However, because all Visual Basic arrays now have a lower
bound of zero (0), the function simply returns a value of 0. The UBound and LBound
functions have the syntax
LBound(ArrayName)
UBound(ArrayName)
where ArrayName is the name of an array that’s been declared in the project.
Use a fixed-size array
- Start Visual Studio, and create a new Visual Basic Windows Forms Application project
named My Fixed Array. - Draw a text box object on the form.
- Set the Multiline property of the TextBox1 object to True so that you can resize the
object. - Resize the text box object so that it fills up most of the form.
- Draw two wide button objects on the form below the text box object, oriented one
beside the other. - Set the following properties for the form and its objects:
Object Property Setting
TextBox1 ScrollBars Vertical
Button1 Text “Enter Temps”
Button2 Text “Display Temps”
Form1 Text “Fixed Array Temps”