• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

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
0
Jason Evans
Asked:
Jason Evans
1 Solution
 
jjmartinCommented:
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
 
caraf_gCommented:
wrong answer.


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
 
caraf_gCommented:

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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
Sorry, never mind the sub.

Dim aArray(3,4) As String

 If ArrayDimensions(aArray) = 2 Then
       
     DoSomething

   Else
       
     DoSomethingElse

 End If

0
 
amebaCommented:
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
 
caraf_gCommented:
Nice one!
0
 
amebaCommented:
Must be - comes from Italy. :)
0
 
Jason EvansSenior Software DeveloperAuthor Commented:
The example code on this site works a treat. Thanks
0
 
amebaCommented:
Thanks for your points
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now