[AccessD] Test whether Array initialised

Gustav Brock Gustav at cactus.dk
Sat Jun 20 08:29:16 CDT 2009


Hi Stuart

Does that work with A97? If I run this:

  Dim aryDemo()
  Debug.Print Not Not aryDemo()

an error (mismatch) is raised.

I use this function for the purpose which just catches the error:

<code>
Public Function IsReDimmed(ByRef varArrayToCheck) As Boolean

' Checks a dynamic array to see if it has been ReDim'med.
' This is to prevent errors before applying LBound() or UBound().
'
' Returns False if varArrayToCheck is dynamic and
' never has been ReDim'med or just has been erased.
'
' 2001-08-11. Cactus Data ApS, CPH. Gustav Brock.

  Dim booIsReDimmed As Boolean
  
  On Error GoTo Err_IsReDimmed
   
  If IsArray(varArrayToCheck) = True Then
    ' Check if varArrayToCheck is ReDim'med.
    ' LBound() will fail if varArrayToCheck not has been ReDim'med.
    booIsReDimmed = LBound(varArrayToCheck) Imp True
    ' No error raised; varArrayToCheck is ReDim'med.
  Else
    ' varArrayToCheck is not an array.
  End If
  
  IsReDimmed = booIsReDimmed
  
Exit_IsReDimmed:
  Exit Function
  
Err_IsReDimmed:
  Select Case Err
    Case 9
      ' Subscript (array index) out of range.
      ' varArrayToCheck is dynamic and not ReDim'med.
    Case Else
      ' Other error.
  End Select
    ' Exit function returning False.
  Resume Exit_IsReDimmed

End Function
</code>

Note the use of Imp, one of the few cases I've found a use for this operator.

/gustav


>>> stuart at lexacorp.com.pg 20-06-2009 14:43:02 >>>
I've been doing quite a bit with arrays in the last couple of days using 
....
ReDim Preserve myArray(UBound(MyArray()) +1)
....

This works fine it the array has been  initialized, but throws an error the first time round 
because the array has not been initialized.  I've just come across a neat hack to test whether 
the array has been initialized:

"IF NOT NOT myArray()" ...  

This takes the 32-bit pointer value in the array variable, mirror the bits, and then mirror them 
again.  If the array has not been initialized, the pointer is 0 so it returns false, if it has been, 
it returns the pointer value. Since this is non-zero, it returns true. 

-- 
Stuart 





More information about the AccessD mailing list