Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting the number of dimensions in an array on the fly?
#6
Something like this might be a little better for this type routine:

Code: (Select All)
Dim a(2, 2, 2) As Integer ' Example: a 3-dimensional array


Print "Number of dimensions: "; CountDimensions(a())
Sleep
Print UBound(a, 6) 'to show that error reports now work as expected
End

ErrorHandler:
If Err = 9 Then ' Subscript out of range error
    exitcount = _TRUE
    Resume Next ' Continue execution after the error
Else
    Print "An unexpected error occurred: "; Err
    End
End If

Function CountDimensions (arr() As Integer)
    On Error GoTo ErrorHandler
    Shared exitcount
    exitcount = _FALSE
    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)
    Loop Until exitcount
    CountDimensions = numDimensions - 1 'subtract 1 as the last 1 was the error
    On Error GoTo 0 'turn off error handling so we get proper error reports
End Function
Reply


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

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

Forum Jump:


Users browsing this thread: 1 Guest(s)