Re: subscript out of range



Jonathan Wood wrote:
>> There is, actually, by examining the SAFEARRAY descriptor:
>
> Sheesh, if that seems like a better approach to the OP than a simple
> On Error Goto, I suggest he takes up C instead of VB. <gd&r>

Isn't life funny sometimes? I needed to do this just today. Simple case. VB6
function that returns an array. Usually. Unless the input is invalid. Well, I
can't test Ubound() on the return. Adding an error trap would be "very messy" in
what's supposed to be fairly tight code here. So instead, I just whapped this
together (based on something I found on Randy's site):

Public Function ArrayDimensions(arr As Variant) As Integer
Dim lplpSafeArray As Long
Dim lpSafeArray As Long

' Address of SafeArray pointer.
Call CopyMemory(lplpSafeArray, ByVal VarPtr(arr) + 8&, ByVal 4&)
If lplpSafeArray Then

' Address of the SafeArray structure
Call CopyMemory(lpSafeArray, ByVal lplpSafeArray, ByVal 4&)
If lpSafeArray Then

' First two bytes in SafeArray tell us the number of dims.
Call CopyMemory(ArrayDimensions, ByVal lpSafeArray, 2&)
End If
End If
End Function

Public Function ArrayInitialized(arr As Variant) As Boolean
' If the array has 1 or more dims, it's initialized.
ArrayInitialized = (ArrayDimensions(arr) > 0)
End Function

Seems to work. <g>
--
Working Without a .NET?
http://classicvb.org/petition


.



Relevant Pages

  • Re: Type mismatch using Split
    ... Dim days As Variant ' The days portion of the interval ... Dim frac As Variant ' The hhmmss portion of the interval ... Public Function FmtN2UAs String ...
    (microsoft.public.vb.general.discussion)
  • Re: Type mismatch using Split
    ... Dim days As Variant ' The days portion of the interval ... Dim frac As Variant ' The hhmmss portion of the interval ... Public Function FmtN2UAs String ...
    (microsoft.public.vb.general.discussion)
  • How do I add results produced by the greatest macro?
    ... rather then seem to simply concatenate ... Public Function GreatestAs Variant) As ... Dim max As Variant ...
    (microsoft.public.access.queries)
  • Re: Type mismatch calling a COM object developed in vb6
    ... Now how do I pass an array of ProdOrd to a function in the COM object?: ... Public Function checkav(session As Variant, requestlist As Variant, ... dim arrResultas ProdOrd ...
    (comp.lang.basic.visual.misc)
  • Re: Decimal to Longs
    ... Dim vNbr1 as Variant ... Public Function GetHiLoWord(ByVal DWord As Long, ByRef LoWord As Long, ByRef ...
    (microsoft.public.vb.general.discussion)

Loading