Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

288 Part II Programming Fundamentals


Using ReDim for Three-Dimensional Arrays


A more complex example involving a three-dimensional array uses a similar syntax. Imagine
that you want to use a three-dimensional, single-precision, floating-point array named
myCube in your program. You can declare the myCube array by using the following syntax:

Dim myCube(,,) As Single

You can then redimension the array and add data to it by using the following code:

ReDim myCube(25, 25, 25)
myCube(10, 1, 1) = 150.46

after which you can expand the size of the third dimension in the array (while preserving the
array’s contents) by using this syntax:

ReDim Preserve myCube(25, 25, 50)

In this example, however, only the third dimension can be expanded—the first and
second dimensions cannot be changed if you redimension the array by using the Preserve
keyword. Attempting to change the size of the first or second dimension in this example
produces a run-time error when the ReDim Preserve statement is executed.

Experiment a little with ReDim Preserve, and see how you can use it to make your own arrays
flexible and robust.

One Step Further: Processing Large Arrays


by Using Methods in the Array Class


In previous sections, you learned about using arrays to store information during program
execution. In this section, you’ll learn about using methods in the Array class of the .NET
Framework, which you can use to quickly sort, search, and reverse the elements in an array,
as well as perform other functions. The sample program I’ve created demonstrates how
these features work especially well with very large arrays. You’ll also learn how to use the
ProgressBar control.

The Array Class


When you create arrays in Visual Basic, you are using a base class that is defined by Visual
Basic for implementing arrays within user-created programs. This Array class also provides
a collection of methods that you can use to manipulate arrays while they are active in
programs. The most useful methods include Array.Sort, Array.Find, Array.Reverse, Array
.Copy, and Array.Clear. You can locate other interesting methods by experimenting with
the Array class in the Code Editor (by using Microsoft IntelliSense) and by checking the
Free download pdf