294 Part II Programming Fundamentals
The code that produced this result is the Button3_Click event procedure, which contains
the following program statements:
'Reverse the order of array elements using Array.Reverse
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer
TextBox1.Text = ""
Array.Reverse(RandArray)
For i = 0 To UBound(RandArray)
TextBox1.Text = TextBox1.Text & RandArray(i) & vbCrLf
ProgressBar1.Value = i 'move progress bar
Next i
End Sub
This event procedure is identical to the Button2_Click event procedure, with the
following exception:
Array.Sort(RandArray)
has become:
Array.Reverse(RandArray)
- Click the Stop Debugging button to end the program.
- Scroll to the top of the Code Editor, and locate the program statement that declares the
RandArray array:
Dim RandArray(0 To 499) As Long
- Replace 499 in the array declaration statement with 2999.
The statement now looks like this:
Dim RandArray(0 To 2999) As Long
- Run the program again to see how declaring and filling an array with 3,000 elements
affects program performance.
Because processing 3,000 elements is much more work, Visual Basic takes a little while
to update the text box object again and again as you fill, sort, and reverse RandArray.
However, the progress bar keeps you posted, and you can see that with just a small
change, you can adapt what you’ve learned in this chapter to different situations.
(The secret was using the UBound function to report the size of the array to the
program’s event procedures, rather than “hard coding” the upper bound at 499 .)
You can further experiment with this program by adding a Randomize statement to the
Form1_Load event procedure (to make the results truly random each time that you run the
program), or by trying additional array sizes and array types. (Try an array size of 100, 800,
2,000, or 5,000 elements, for example .) If you try larger numbers, you’ll eventually exceed the
amount of data that the text box object can display, but it takes a while before you exceed
the maximum array size allowed by Visual Basic.