DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Does Diadem have a function to determine the number of dimensions in an array?

Solved!
Go to solution

I'm working with a matrix, and I want to make sure it's actually a matrix.  If I use Ubound(MyMatrix,2), I get an error if the second dimension is not declared.  I suppose I could catch the error if Ubound creates one, but I cannot find a method in VB to return the number of dimensions of a matrix. Does diadem have any such function? 

0 Kudos
Message 1 of 6
(4,266 Views)
Solution
Accepted by topic author RussellSenior

You can use a few lines of vbs to determine the dimension. VBS does not offer the functionallity as far as I know.

 

Function GetArrayDimension(ByVal arr)
   GetArrayDimension = null
   If IsArray(arr) Then
     Dim i : For i = 1 To 60
       On Error Resume Next
       UBound arr, i
       If Err.Number <> 0 Then
         GetArrayDimension = i-1
         Exit Function
       End If
     Next
     GetArrayDimension = i
   End If
 End Function
Message 2 of 6
(4,243 Views)

Thanks.  That's what I ended up implementing. Don't you have to use "On Error Goto 0" to turn error handling back on?

0 Kudos
Message 3 of 6
(4,241 Views)

Hi Russell,

 

If you don't use "On Error Goto 0" afterwards, the rest of your code will run with error messages suppressed, and you will find it impossible to debug.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 4 of 6
(4,233 Views)

Hmm potentially I should have inserted an

on error goto 0

but it is not necessary. What might be wrong is that err.number does not end up with 0 but it does not influence the rest of the script if err.number is not checked.

 

on error goto 0

does two things. Reset the err.number to 0 and set the local error handler back to raise.

Local means that there is one for each level. If a function/sub does call

on error resume next

this does not affect called functions/sub nor does it effect the calling code.

Run the following example to have look. cr raises an error even if it was switched off by c and the final division by zero in the main code also shows up.

 

Option Explicit

dim txt

txt = txt & "e: " & err.number & VBCRLF
err.Clear
txt = txt & "e: " & err.number & " a:" & a() & " e: " & err.number & VBCRLF
err.Clear
txt = txt & "e: " & err.number & " b:" & b() & " e: " & err.number & VBCRLF
err.Clear
txt = txt & "e: " & err.number & " c:" & c() & " e: " & err.number & VBCRLF
err.Clear


MsgBox Txt

dim res : res = 1 / 0


function a()

  on error resume next
  dim res : res = 1 / 0
  a = err.number
  on error goto 0

end function

function b()

  on error resume next
  dim res : res = 1 / 0
  b = err.number

end function

function c()

  on error resume next
  c = cr()

end function

function cr()

  dim res : res = 1 / 0

  cr = 101

end function

 

0 Kudos
Message 5 of 6
(4,177 Views)

Be aware that the err.number is also set back during initialize

Option Explicit 

err.number = 4711
MsgBox err.number
on error resume next
MsgBox err.number

will show 0 in the second box

0 Kudos
Message 6 of 6
(4,175 Views)