Change the Dimension of an Array


Use ReDim to change the dimensions of an existing array by using the same syntax as the DIM statement. Increasing the dimension of an array while using the keyword Preserve preserves all of the data, but decreasing the dimension causes data to be lost by truncation . Unlike some variants of BASIC, OOo Basic allows all dimensions of an array to be changed while also preserving data.

The primary use of the ReDim statement is to change the dimension of an existing array. If you know the size of the array that you will need, you can declare it when you declare the variable. If you don't know the size ahead of time, you can declare the array with any size , including as an empty array, and then change the dimension when you know it.

 Dim v() As Integer Dim x(4) As Integer i% = 7 ReDim v(3*i%) As Integer       'Same as Dim v(0 To 21) As Integer ReDim x(i%, 1 To 4) As Integer 'Same as Dim x(0 To 7, 1 To 4) 

The ReDim statement changes the dimension of an existing array, even an empty one. ReDim specifies both the dimensions and the type. The type specified with the ReDim statement must match the type specified when the variable is declared. If the types differ , you'll see the compile-time error "Variable already defined."

 Dim a() As Integer            'Empty Integer Array Dim v(8)                      'Variant array with nine entries ReDim v()                     'v() is a valid empty array ReDim a(2 To 4, 5) As Integer 'a() is a two-dimensional array 
Tip  

In OOo 2.0, the type will no longer be required with the ReDim statement. For example, the statement "ReDim a(2 To 4, 5)" will be valid.

ReDim correctly creates an empty array, but Array and DimArray do not. In version 2.0, the behavior will be consistent with a valid empty array created by all methods .

The DimArray function creates and returns a dimensioned Variant array that contains no data. This is not useful if you require an array of a specific type, or if you simply need to change the dimensions of an existing array while preserving data. The ReDim statement changes the dimensions of an existing array with the option of preserving existing data. You can use the ReDim statement to change a dimensioned array to an empty array.

 v = Array()       'Bug in OOo Basic returns an invalid empty array ReDim v()         'Now it is a valid empty array, no bug with ReDim Print LBound(v)   'No run-time errors here 

The subroutine in Listing 6 contains many examples of the ReDim statement using the keyword Preserve. Figure 1 shows the results of these commands.

click to expand
Figure 1: Use ReDim to change the dimensions of an array.
Listing 6: ExampleReDimPreserve is found in the Array module in this chapter's source code files as SC04.sxw.
start example
 Sub ExampleReDimPreserve   Dim a(5) As Integer     'A dimensioned array, 0 To 5   Dim b()                 'An empty array of type Variant   Dim c() As Integer      'An empty array of type Integer   Dim s$                  'The string that accumulates the output text   REM a is dimensioned from 0 to 5 where a(i) = i   a(0) = 0 : a(1) = 1 : a(2) = 2 : a(3) = 3 : a(4) = 4 : a(5) = 5   s$ = "a() at start = " & Join(a()) & CHR$(10)   REM a is dimensioned from 1 to 3 where a(i) = i   ReDim Preserve a(1 To 3) As Integer   s$ = s$ & "ReDim Preserve a(1 To 3) = " &  Join(a()) & CHR$(10)   ReDim a() As Integer   s$ = s$ & "ReDim a() has LBound = " &_        LBound(a()) & " UBound = " & UBound(a()) & CHR$(10)   REM Array() returns a Variant type   Rem b is dimensioned from 0 to 9 where b(i) = i+1   b = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)   s$ = s & CHR$(10) & "b() at start = " & Join(b()) & CHR$(10)   REM b is dimensioned from 1 to 3 where b(i) = i+1   Dim il%, iu%   il = 1 : iu = 3   ReDim Preserve b(il To iu)   s$ = s$ & "ReDim Preserve b(1 To 3) = " &  Join(b()) & CHR$(10)   ReDim b(-5 To 5)   s$ = s$ & "ReDim b(-5 To 5) = " &  Join(b()) & CHR$(10)   s$ = s$ & "ReDim b(-5 To 5) has LBound = " &_        LBound(b()) & " UBound = " & UBound(b()) & CHR$(10) & CHR$(10)   ReDim b(-5 To 5, 2 To 4)   s$ = s$ & "ReDim b(-5 To 5, 2 To 4) has dimension 1 LBound = " &_        LBound(b()) & " UBound = " & UBound(b()) & CHR$(10)   s$ = s$ & "ReDim b(-5 To 5, 2 To 4) has dimension 2 LBound = " &_        LBound(b(), 2) & " UBound = " & UBound(b(), 2) & CHR$(10)   MsgBox s$, 0, "ReDim Examples" End Sub 
end example
 
Compatibility  

Visual Basic has different rules for changing the dimensions of an array, and these rules change between versions of VB. As a general rule, OOo Basic is more flexible, allowing all dimensions to change while using the keyword Preserve, rather than changing just the last dimension, as in VB.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

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