A.D.Tejpal
adtp at airtelmail.in
Sat Jun 20 13:13:18 CDT 2009
Gustav, Stuart,
The function given below could also be considered:
'================================
Function Fn_IsArrayInitialized(ByVal varArray _
As Variant) As Boolean
On Error Resume Next
Fn_IsArrayInitialized = _
Not IsError(UBound(varArray) >= 0)
End Function
'================================
Best wishes,
A.D. Tejpal
------------
----- Original Message -----
From: Gustav Brock
To: accessd at databaseadvisors.com
Sent: Saturday, June 20, 2009 18:59
Subject: Re: [AccessD] Test whether Array initialised
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
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com