IsArraySafe |
Description: | |
If you use 'Dim nArray()' and later want to see if you've redimensioned the array, or if it's still empty, traditionally you would just use some error logic and attempt to read from it. This bit of code demonstrates using CopyMemory to look into the VARIANT and SAFEARRAY structures to see if the array actually contains any dimensions. I present it, because it's actually a bit faster than using the error logic, and could save some runtime if you call such subroutine quite a bit. | |
Code: | |
Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (dest As Any, src As Any, ByVal length As Long) As Long Public Function IsArraySafe(ArrayVal As Variant) As Boolean If (VarType(ArrayVal) And vbArray) Then Dim lpSafeArray As Long Dim nDimensions As Long CopyMemory lpSafeArray, ByVal VarPtr(ArrayVal) + 8, 4 CopyMemory nDimensions, ByVal lpSafeArray, 2 If nDimensions > 0 Then IsArraySafe = True Else IsArraySafe = False End If Else Debug.Assert False 'ArrayVal doesn't look to contain an Array End If End Function | |
Sample Usage: | |
If Not IsArraySafe(nArray()) Then Redim nArray(100) End If |