Preserving Array Contents by Using ReDim Preserve


Preserving Array Contents by Using ReDim Preserve

In the previous exercise, you used the ReDim statement to specify the size of a dynamic array at run time. However, one potential shortcoming associated with the ReDim statement is that if you redimension an array that already has data in it, all the existing data is irretrievably lost. After the ReDim statement is executed, the contents of a dynamic array are set to their default value, such as zero or null. Depending on your outlook, this can be considered a useful feature for emptying the contents of arrays, or it can be an irksome feature that requires a workaround.

Fortunately, Visual Basic 2005 provides the same useful feature that Visual Basic 6 provides for array redimensioning, the Preserve keyword, which you use to preserve the data in an array when you change its dimensions. The syntax for the Preserve keyword is as follows:

ReDim Preserve ArrayName(Dim1Elements, Dim2Elements, ...)

In such a ReDim statement, the array must continue to have the same number of dimensions and contain the same type of data. In addition, there's a caveat that you can resize only the last array dimension. For example, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. (Single-dimension arrays automatically pass this test, so you can freely expand the size of dynamic arrays by using the Preserve keyword.)

The following examples show how you can use Preserve to increase the size of the last dimension in a dynamic array without erasing any existing data contained in the array.

If you originally declared a dynamic string array named Philosophers by using the syntax

Dim Philosophers() As String

you can redimension the array and add data to it by using code similar to the following:

ReDim Philosophers(200) Philosophers(200) = "Steve Harrison"

You can expand the size of the Philosophers array to 301 elements (0–300) and preserve the existing contents, by using the following syntax:

ReDim Preserve Philosophers(300)

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.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net