Array Inspection Functions


The most fundamental thing to ask about an array is whether or not it really is an array. The IsArray function returns True if the argument is an array, and False otherwise . Use the LBound and UBound functions to determine the lower and upper bounds of an array. An array is empty if the upper bound is less than the lower bound.

The first argument to LBound and UBound is the array to check. The second optional argument is an integer expression specifying which dimension is returned. The default value is 1, which returns the lower bound of the first dimension.

 Dim a() Dim b(2 to 3, -5 To 5) Print LBound(a())    ' 0 Print UBound(a())    '-1 because the array is empty Print LBound(b())    ' 2 no optional second argument so defaults to 1 Print LBound(b(),1)  ' 2 optional second argument specifies first dimension Print UBound(b(),2)  ' 5 
Bug  

As of OOo version 1.1, some functions return arrays with a zero dimension. This causes a run-time error when passed as an argument to LBound and UBound. For example, LBound(Array()) causes a run-time error. This will be fixed in OOo version 2.0.

If the value of the second argument doesn't contain a valid value, if it's greater than the number of dimensions, or if it's less than 1, a run-time error occurs. As of OOo version 1.1.1, some functions, such as DimArray and Array, return an array with zero dimensions. It is not possible to use LBound() or UBound() on these zero-dimensional arrays. This is because zero is not a valid dimension to check, and the LBound and UBound functions generate a runtime error if the argument is out of bounds. Use the On Error statement to avoid this problem. See Listing 7 .

Listing 7: SafeUBound is found in the Array module in this chapter's source code files as SC04.sxw.
start example
 Function SafeUBound(v, Optional n) As Integer   SafeUBound = -1               'If an error occurs, this is already set   On Error GoTo BadArrayFound   'On error skip to the end   If IsMissing(n) Then          'Was the optional argument used?     SafeUBound = UBound(v)   Else     SafeUBound = UBound(v, n)   'Optional argument is present   End If BadArrayFound:                  'Jump here on error   On Error GoTo 0               'Turn off this error handler End Function 
end example
 

The macro in Listing 7 properly returns -1 if an error occurs. The proper value is returned for invalid empty arrays, but it also returns -1 if the first argument isn't an array or if the second argument is simply too large. The ArrayInfo function in Listing 8 uses a similar technique to return array information about a variable. Also see Figure 2 .

click to expand
Figure 2: Use proper error handling to determine the dimension of the array.
Listing 8: ArrayInfo is found in the Array module in this chapter's source code files as SC04.sxw.
start example
 REM If the first argument is an array, the dimensions are determined. REM Special care is given to an empty array that was created using DimArray REM or Array. REM a     : Variable to check REM sName : Name of the variable for a better looking string Function arrayInfo(a, sName$) As String   REM First, verify that:   REM the variable is not NULL, an empty Object   REM the variable is not EMPTY, an uninitialized Variant   REM the variable is an array.   If IsNull(a) Then     arrayInfo = "Variable " & sName & " is Null"     Exit Function   End If   If IsEmpty(a) Then     arrayInfo = "Variable " & sName & " is Empty"     Exit Function   End If   If Not IsArray(a) Then     arrayInfo = "Variable " & sName & " is not an array"     Exit Function   End If   REM The variable is an array, so get ready to work   Dim s As String                  'Build the return value in s   Dim iCurDim As Integer           'Current dimension   Dim i%, j%                       'Hold the LBound and UBound values   On Error GoTo BadDimension       'Set up the error handler   iCurDim = 1                      'Ready to check the first dimension   REM Initial pretty return string   s = "Array dimensioned as " & sName$ & "("   Do While True                      'Loop forever     i = LBound(a(), iCurDim)         'Error if dimension is too large or     j = UBound(a(), iCurDim)         'if invalid empty array     If i > j Then Exit Do             'If empty array then get out     If iCurDim > 1 Then s = s &       'Separate dimensions with a comma     s = s & i & " To " & j            'Add in the current dimensions     iCurDim = iCurDim + 1             'Check the next dimension   Loop   REM Only arrive here if the array is a valid empty array.   REM Otherwise, an error occurs when the dimension is too   REM large and a jump is made to the error handler   REM Include the type as returned from the TypeName function.   REM The type name includes a trailing "()" so remove this   s = s & ") As " & Left(TypeName(a), LEN(TypeName(a))-2)   arrayInfo = s   Exit Function BadDimension:   REM Turn off the error handler   On Error GoTo 0   REM Include the type as returned from the TypeName function.   REM The type name includes a trailing "()" so remove this   s = s & ") As " & Left(TypeName(a), LEN(TypeName(a))-2)   REM If errored out on the first dimension then this must   REM be an invalid empty array.   If iCurDim = 1 Then s = s & " *** INVALID Empty Array"   arrayInfo = s End Function Sub UseArrayInfo   Dim i As Integer, v   Dim ia(1 To 3) As Integer Dim sa() As Single   Dim m(3, 4, -4 To -1)   Dim s As String   s = s & arrayInfo(i, "i") & CHR$(10)            'Not an array   s = s & arrayInfo(v, "v") & CHR$(10)            'Empty variant   s = s & arrayInfo(sa(), "sa") & CHR$(10)        'Empty array   s = s & arrayInfo(Array(), "Array") & CHR$(10)  'BAD empty array   s = s & arrayInfo(ia(), "ia") & CHR$(10)   s = s & arrayInfo(m(), "m") & CHR$(10)   MsgBox s, 0, "Array Info" End Sub 
end example
 

An array with one dimension may have an upper bound that is less than the lower bound. This indicates that the array has no allocated spots for data. This is different than an array that has data locations allocated but no data has been saved in them. For most data types, such as Integer, if space is allocated for an integer, then it has a value.

 Dim a(3) As Integer  'This array has four integer values, they are all zero Dim b(3)             'This array has four Variants, they are all Empty Dim c()              'This array has one dimension and no space Ubound < Lbound v = Array()          'This array has zero dimensions, Ubound and LBound fail. 



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