# Find out number of array dimensions.

Say I have a routine that accepts an array as a parameter, how can I find out the number of dimensions that array is.

i.e. Dim aArray(3,4) As String

Public Sub Foo(aArray() As String)

If NumOfDims(aArray()) = 2 Then

DoSomething

Else

DoSomethingElse

End If

End Sub
LVL 10
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
ubound function:
Returns the largest available subscript for the indicated dimension of an array.

lbound function:
Returns the smallest available subscript for the indicated dimension of an array.
0
Commented:

Private Function ArrayDimensions(arrtest As Variant) As Long

On Error GoTo ExitFun

Dim lngDimensions As Long
Dim lngTest As Long

Do While True
lngDimensions = lngDimensions + 1
lngTest = LBound(arrtest, lngDimensions)
Loop

ExitFun:
ArrayDimensions = lngDimensions - 1

End Function

0
Commented:

Public Sub Foo(aArray As Variant)

Dim aArray(3,4) As String

If ArrayDimensions(aArray) = 2 Then

DoSomething

Else

DoSomethingElse

End If

End Sub

0
Commented:
Sorry, never mind the sub.

Dim aArray(3,4) As String

If ArrayDimensions(aArray) = 2 Then

DoSomething

Else

DoSomethingElse

End If

0
Commented:
caraf_g said the first way (LBound)
but maybe you are brave enough to use the second way:
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=27
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Nice one!
0
Commented:
Must be - comes from Italy. :)
0
Senior Software DeveloperAuthor Commented:
The example code on this site works a treat. Thanks
0
Commented: