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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Other methods to tell if array has been allocated yet? 2

Status
Not open for further replies.

Jasen

IS-IT--Management
May 1, 2002
435
US
Right now I use an error trapping trick to do it, like this:
Code:
Public Function TestUBound(myArray As Variant) As Long
On Error Resume Next

    Dim size As Long

    size = UBound(myArray)
    If Err = 9 Then
        TestUBound = -1
    Else
        TestUBound = size
    End If

End Function
But I'm still annoyed VB has no way to look at an empty array or count the number of dimensions easily.
Are there any other tricks you guys use to get this done?
 
See strongm's post in Thread222-836312. Works great, I use it often.

zemp
 
Try this function,

Function NumberOfDims(arr As Variant) As Integer
Dim dummy As Long
On Error Resume Next
Do
dummy = UBound(arr, NumberOfDims + 1)
If Err Then Exit Do
NumberOfDims = NumberOfDims + 1
Loop
End Function

Taken from Programming Microsoft Visual Basic 6.0 by Francesco Balena

It will return the number of dimensions of an array or zero if it is not an array.

Hope this helps.

Andy
 
Yup, but that still uses the error trapping trick; [bjasen[/b] was wondering if there were an alternative to this
 
No,

He didn't say that at all.

Quote:
Are there any other tricks you guys use to get this done?

And the function I posted does what he asks for 'look at an empty array or count the number of dimensions easily.'

So we have 2 solutions for the same problem. I would also venture to say that my solution (although borrowed which I have given credit for) is easier for most developers to follow.

Andy
 
HActually he says:

>Right now I use an error trapping trick to do it ... Are there any other tricks you guys use to get this done?
 
(having said that, the Balena solution is, like much of his stuff, an elegant one - and, as I say in the thread indicated by zemp, I'm not convinced that the alternative I present is "is 'better' than throwing an exception". However so many people ask for an alternative that I thought I'd see if one could be done)
 
You guys are both right. I was interested in all alternatives.
My code is just an extended UBound function that returns -1 if there is no UBound.

Andy's method error traps like mine, but it returns the dimensions of the array, which is something I definitely wanted to see.

strongm/zemp once again provides a very interesting answer.
So you check the first 4 bytes of the array, and if not zero then it has elements?
 
No ...

Before I explain it, here's a longer version that provides the same results as Balena's code which may provide additional hints as to what I'm actually doing:
Code:
[blue]Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Private Declare Function VarPtrArray Lib "msvbvm60.DLL" Alias "VarPtr" (Ptr() As Any) As Long
Private Type SafeArray
    cDims As Integer
    fFeatures As Integer
    cbElements As Long
    cLocks As Long
    pvData As Long
End Type

Public Function NumberOfDims(arrPointer As Long) As Long
    Dim lSourceAddress As Long
    Dim sSource As SafeArray
    
    CopyMemory lSourceAddress, ByVal arrPointer, 4 ' This is evil code
    If lSourceAddress Then
        CopyMemory ByVal sSource, ByVal lSourceAddress, Len(sSource)
        NumberOfDims = sSource.cDims
    End If
End Function[/blue]
 
So the first 4 bytes of an array is an address pointer itself, which then points to where the number of dimensions are?
 
A VB array points to the underlying SAFEARRAY structure (although VB tries to hide this away from you quite effectively, hence the need for the VarPtrArray function - and, indeed, VarPtrStringArray if you are dealing with an array of strings)
 
So the first 4 bytes points to it's safearray, and the first 2 bytes of the safearray are the actual dimension count.
I think you get the same answer by simply making sSource an integer instead of the UDT, and doing the second copymemory call like
CopyMemory sSource, ByVal lSourceAddress, 2
 
Sure. But firstly, doing that wouldn't have really helped explain anything, and secondly , but I put this additional example together quickly by cutting and pasting from some code that was doing something else with SAFEARRAYs
 
strongm

You are right of course (in what was originally asked).

Hope i did'nt bite too much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top