21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

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.

Determining the Dimensions of Data Fields<br />

Arrays<br />

Functions LBound() and UBound() return respectively the lowest permitted index value and the highest permitted<br />

index value of an array. This is useful when an array has changed its dimensions.<br />

Dim MyArray(10) As Integer<br />

' ... some instructions<br />

Dim n As Integer<br />

n = 47 ' could be the result of a computation<br />

Redim MyArray(n) As Integer<br />

MsgBox(LBound(MyArray)) ' displays : 0<br />

MsgBox(UBound(MyArray)) ' displays : 47<br />

For a multi-dimensional array you need to specify the position (1 to n) of the index you want to know the<br />

permitted lower and upper values:<br />

Dim MyArray(10, 13 to 28) As Integer<br />

MsgBox(LBound(MyArray, 2)) ' displays : 13<br />

MsgBox(UBound(MyArray, 2)) ' displays : 28<br />

Empty arrays<br />

In some cases, especially when dealing with the API, you need to declare an empty array. Such array is declared<br />

without dimension, but may later be filled by an API function or with a Redim statement:<br />

Dim s() As String ' declare an empty array<br />

' --- later in the program ...<br />

Redim s(13) As String<br />

You cannot assign a value to an empty array, since it does not contain any elements.<br />

The "signature" of an empty array is that UBound() returns -1 and LBound() returns 0:<br />

Dim MyArray() As Integer<br />

MsgBox(LBound(MyArray)) ' displays : 0<br />

MsgBox(UBound(MyArray)) ' displays : -1<br />

Some API functions return an array containing elements (indexed from zero) or return an empty array. Use<br />

UBound() to check if the returned array is empty.<br />

Defining values for arrays<br />

Values for the Array fields can be stored like this:<br />

MyArray(0) = "somevalue"<br />

Accessing Arrays<br />

Accessing values in an array works like this:<br />

MsgBox("Value:" & MyArray(0))<br />

Array Creation, value assignment and access example<br />

And example containing all steps that show real array usage:<br />

Sub TestArrayAxess Dim MyArray(3) MyArray(0) = "lala" MsgBox("Value:"<br />

& MyArray(0))End Sub<br />

Chapter 2 · The Language of <strong>OpenOffice</strong>.<strong>org</strong> <strong>BASIC</strong> 19

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

Saved successfully!

Ooh no, something went wrong!