Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting the number of dimensions in an array on the fly?
#2
We have to dim the arrays, so...

Dim a(2, 3, 4) As Integer ' Example: a 3-dimensional array
Print UBound(a)
Print UBound(a, 2)
Print UBound(a, 3)

Results:
2
3
4

So using ubound gets the indicies at runtime. What are you looking to use this for?

I mean if you don't do a dim, you just get the default 10 value...

a(1, 1, 1) = 500
Print UBound(a)
Print UBound(a, 2)
Print UBound(a, 3)

Results:
10
10
10

I think what you are getting at is how to determine if the array is multidimensional. Yeah, the Google code should be...

Code: (Select All)
Dim a(2, 3, 4) As Integer ' Example: a 3-dimensional array
Print UBound(a)
Print UBound(a, 2)
Print UBound(a, 3)
Dim Shared numDimensions As Integer

On Error GoTo ErrorHandler

Call CountDimensions(a())

End

ErrorHandler:
If Err = 9 Then ' Subscript out of range error
    End
Else
    Print "An unexpected error occurred: "; Err
    End
End If

Sub CountDimensions (arr() As Integer)
    numDimensions = 0
    Do
        numDimensions = numDimensions + 1
        ' Attempt to get the upper bound of the current dimension
        ' If this dimension doesn't exist, an error will occur
        dummy = UBound(arr, numDimensions)
        Print "dummy ="; dummy
    Loop
End Sub

I suppose a way around it is to just assign variables to the array, and track tose instead of doing an error trap...

Code: (Select All)
Dim Shared As Integer d1, d2, d3
d1 = 2: d2 = 3
Dim a(d1, d2) As Integer ' Example: a 3-dimensional array

Print "UBound Indicies #1 ="; d1;: If d1 Then Print UBound(a, 1) Else Print
Print "UBound Indicies #2 ="; d2;: If d2 Then Print UBound(a, 2) Else Print
Print "UBound Indicies #3 ="; d3;: If d3 Then Print UBound(a, 3) Else Print

And maybe I just missed the point entirely. Let me know if that's the case, but this is where my old tired mind wondered off to... and now if you will excuse me, I have my foot caught in a rabbit hole!

Pete
Reply


Messages In This Thread
RE: getting the number of dimensions in an array on the fly? - by Pete - 09-15-2025, 09:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  _NEWIMAGE can't accept variables as dimensions? bobalooie 23 915 02-18-2026, 11:16 PM
Last Post: Unseen Machine
  Maximum Number of Attachments per Thread Magdha 7 445 01-13-2026, 10:13 AM
Last Post: Magdha
  Random Number Generator pmackay 14 1,231 07-30-2025, 12:56 PM
Last Post: SMcNeill
  Illegal string-number conversion Herve 7 755 07-07-2025, 09:53 AM
Last Post: a740g
  generating a random number in the full range of that number? (Integer, Long) madscijr 2 642 05-01-2025, 09:11 PM
Last Post: madscijr

Forum Jump:


Users browsing this thread: 1 Guest(s)