22.01.2015 Views

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The arguments passed to the Array function become data in the returned Variant array. The DimArray<br />

function, on the other hand, interprets the arguments as the dimensions of an array to create (see Listing 21).<br />

The arguments can be expressions, so a variable can be used to set the dimension.<br />

Listing 21. Redimension array.<br />

Sub ExampleDimArray<br />

Dim a(), i%<br />

Dim s$<br />

a = Array(10, 11, 12)<br />

s = "" & LBound(a()) & " " & UBound(a()) Rem 0 2<br />

a() = DimArray(3)<br />

REM the same as Dim a(3)<br />

a() = DimArray(2, 1)<br />

REM the same as Dim a(2,1)<br />

i = 4<br />

a = DimArray(3, i)<br />

Rem the same as Dim a(3,4)<br />

s = s & CHR$(10) & LBound(a(),1) & " " & UBound(a(),1) Rem 0, 3<br />

s = s & CHR$(10) & LBound(a(),2) & " " & UBound(a(),2) Rem 0, 4<br />

a() = DimArray() REM an empty array<br />

MsgBox s, 0, "Example Dim Array"<br />

End Sub<br />

The Array and DimArray functions both return an array of Variants. The ReDim statement changes the<br />

dimension of an existing array. This can change both individual dimensions and the number of dimensions.<br />

The arguments can be expressions because the ReDim statement is evaluated at run time.<br />

Dim e() As Integer, i As Integer<br />

i = 4<br />

ReDim e(5) As Integer REM Dimension is 1, a size of 0 To 5 is valid.<br />

ReDim e(3 To 10) As Integer REM Dimension is 1, a size of 3 To 10 is valid.<br />

ReDim e(3, i) As Integer REM Dimension is 2, a size of (0 To 3, 0 To 4) is valid.<br />

Some tips regarding arrays:<br />

• LBound and UBound work with empty arrays.<br />

• An empty array has one dimension. The lower bound is zero and the upper bound is -1.<br />

• Use ReDim to cause an existing array to become empty.<br />

The ReDim statement supports the keyword Preserve. This attempts to save the data when the dimensions of<br />

an array are changed. Increasing the dimension of an array preserves all of the data, but decreasing the<br />

dimension causes data to be lost by truncation. Data can be truncated at either end. If an element in the new<br />

array existed in the old array, the value is unchanged. Unlike some variants of BASIC, OOo Basic allows all<br />

dimensions of an array to be changed while still preserving data.<br />

Dim a() As Integer<br />

ReDim a(3, 3, 3) As Integer<br />

a(1,1,1) = 1 : a(1, 1, 2) = 2 : a(2, 1, 1) = 3<br />

ReDim preserve a(-1 To 4, 4, 4) As Integer<br />

Print "(" & a(1,1,1) & ", " & a(1, 1, 2) & ", " & a(2, 1, 1) & ")"<br />

ReDim specifies both the dimensions and an optional type. If the type is included, it must match the type<br />

specified when the variable is declared or OOo generates a compile-time error.<br />

Listing 22 is a utility function that accepts a simple array and returns a string with all of the elements in the<br />

array. The ReDim example code, also in Listing 22, uses ArrayToString.<br />

48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!