Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Has a Dynamic Array been dimentioned?

Status
Not open for further replies.

spinjector

Programmer
Jul 1, 2001
29
US

How do I detect if a dynamic array has been dimentioned without using somthing like an 'On Error' trap?

For instance:
Code:
Dim MyArray() as String
N = UBound(MyArray)  'generates "subscript out of range" error
Redim MyArray(10)
N = UBound(MyArray)  'works and displays "10"
I have tried using functions like IsNull() and IsEmpty(), but none of these seem to work.

I would like something like this:
Code:
Dim MyArray() as String
If Not IsDimentioned(MyArray)
   Redim MyArray(10)
End If
Does anyone have any suggestions?

Thanks.
 
An error trap is the simplest but you can do it in-line
Code:
On Error Resume Next
n = Ubound(myArray)
if Err.Number > 0 then ReDim myArray(10)
 
I prefer an error handler as Golom suggested but another way is to declare the variable as a Variant rather than an array and test it with IsEmpty.

Paul Bent
Northwind IT Systems
 

Thanks, Golom.

Paul, could you give me a bit of example of your idea...? I'm not sure I follow you completely. Thanks.
 
mattshe,

Dim MyArray as Variant
If IsEmpty(MyArray)
Redim MyArray(10)
End If

And yet another way is to use a global or form level boolean variable to flag if the array is dimensioned. Follow a Redim statement by setting the flag to True and an Erase statement by setting it to False.

Paul Bent
Northwind IT Systems
 
Public Function IsArrayDimensioned(varArray As Variant) As Boolean

'*Purpose: Checks if an array has been properly dimensioned
'*Accepts: -varArray: an array which existence is to be verified
'*Returns: a Boolean variable: TRUE if the array has been properly
'* dimensioned, FALSE otherwise
'*Uses: uses the rtlMoveMemory API - to be declared in the
'* general section
'*Remarks: A FIXED array e.g. Dim A(2) is ALWAYS dimensioned
'* even if it has been ERASED!

Dim lngPointerToVariantArray As Long
CopyMemory lngPointerToVariantArray, ByVal VarPtr(varArray) + 8, ByVal 4&

Dim lngPointerToArrayStructure As Long
CopyMemory lngPointerToArrayStructure, ByVal lngPointerToVariantArray, ByVal 4&

Select Case lngPointerToArrayStructure <> 0

Case True
IsArrayDimensioned = True

Case False
IsArrayDimensioned = False

End Select

End Function


Following excerpt prints TRUE in response to the above routine. In the General Section:
Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; _
(lngTarget As Any, lngSource As Any, ByVal lngLengthInBytes As Long)


Somewhere in the mainline code:
Dim a_intA() As String
Dim i As Integer
ReDim a_intA(9)
Debug.Print IsArrayDimensioned(a_intA)





_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top