DimArray Creates Empty Multi-Dimensional Arrays


The DimArray function creates and returns a dimensioned Variant array. This allows the dimensions of the array to be determined at run time. The arguments specify the dimensions of the array; each argument specifies one dimension. If no arguments are present, an empty array is created.

The primary use of the DimArray statement is to create an empty, dimensioned Variant 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 , and if a Variant array is acceptable, then you can create the empty, dimensioned array at run time. See Listing 5 .

Listing 5: DimArray returns a dimensioned Variant array that contains no data.
start example
 i% = 7 v = DimArray(3*i%)   'Same as Dim v(0 To 21) v = DimArray(i%, 4)  'Same as Dim v(0 To 7, 0 To 4) 
end example
 

The code in Listing 5 does not show how the variable v is declared. This works equally well if v is declared as a Variant, or a Variant array. The argument list is a comma-separated list of expressions. Each expression is rounded to an integer and used to set the range of one dimension of the returned array.

 Dim a As Variant Dim v() Dim i As Integer i = 2 a = DimArray(3)                          'Same as Dim a(0 To 3) a = DimArray(1+i, 2*i)                   'Same as Dim a(0 To 3, 0 To 4) v() = DimArray(1)                        'Same as Dim v(0 To 1) v(0) = Array(1, 2, 3)                    'Oh no, not this again! v(1) = Array("one", "two", "three")      'You can do it, but yuck! v = DimArray(1, 2)                       'Now that makes more sense! v(0, 0) = 1      : v(0, 1) = 2     : v(0, 2) = 3 v(1, 0) = "one"  : v(1, 1) = "two" : v(1, 2) = "three" Print v(0, 1)    'prints 2 
Tip  

Option Base 1 has no effect on the dimensions of the array returned by the DimArray function. For each dimension, the lower bound of the range is always zero and the upper bound is the rounded integer value of the relevant expression.

As of OOo version 1.1.1, calling the DimArray function with no arguments returns an array with zero dimensions. The result is that IsArray(Array()) succeeds, but LBound(Array()) causes a runtime error. This behavior is scheduled to change in version 2.0.




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