D
deltaquattro
Hi guys,
I'm not so used to the On Error statement, ssince I come from a
Fortran background where all error handling must be performed using If
Then Else constructs. Example: in this interpolation subroutine
'Returns an interpolated value of x
'doing a lookup of xarr->yarr
Public Function Interp1(xArr() As Double, yArr() As Double, X As
Double) As Double
Dim I As Long
If ((X < xArr(LBound(xArr))) Or (X > xArr(UBound(xArr)))) Then
MsgBox "Interp1: x is out of bound"
Stop
Exit Function
End If
If xArr(LBound(xArr)) = X Then
Interp1 = yArr(LBound(yArr))
Exit Function
End If
'For i = LBound(xArr) To UBound(xArr)
' If xArr(i) >= X Then
' Interp1 = yArr(i - 1) + (X - xArr(i - 1)) / (xArr(i) - xArr(i -
1)) * (yArr(i) - yArr(i - 1))
' Exit Function
' End If
'Next i
I = Locate(xArr, X) + 1
Interp1 = yArr(I - 1) + (X - xArr(I - 1)) / (xArr(I) - xArr(I - 1)) *
(yArr(I) - yArr(I - 1))
End Function
woudl you advise substituting the If...MsgBox combination with an On
Error statement? In general, how can I understand if it's better to
use On Error or to test for errors with If? Finally, a purely
programming style question: would you suggest to include the error
handling in the interpolation subroutine, or to move it in the caller
subroutine? Thanks,
Best Regards
deltaquattro
I'm not so used to the On Error statement, ssince I come from a
Fortran background where all error handling must be performed using If
Then Else constructs. Example: in this interpolation subroutine
'Returns an interpolated value of x
'doing a lookup of xarr->yarr
Public Function Interp1(xArr() As Double, yArr() As Double, X As
Double) As Double
Dim I As Long
If ((X < xArr(LBound(xArr))) Or (X > xArr(UBound(xArr)))) Then
MsgBox "Interp1: x is out of bound"
Stop
Exit Function
End If
If xArr(LBound(xArr)) = X Then
Interp1 = yArr(LBound(yArr))
Exit Function
End If
'For i = LBound(xArr) To UBound(xArr)
' If xArr(i) >= X Then
' Interp1 = yArr(i - 1) + (X - xArr(i - 1)) / (xArr(i) - xArr(i -
1)) * (yArr(i) - yArr(i - 1))
' Exit Function
' End If
'Next i
I = Locate(xArr, X) + 1
Interp1 = yArr(I - 1) + (X - xArr(I - 1)) / (xArr(I) - xArr(I - 1)) *
(yArr(I) - yArr(I - 1))
End Function
woudl you advise substituting the If...MsgBox combination with an On
Error statement? In general, how can I understand if it's better to
use On Error or to test for errors with If? Finally, a purely
programming style question: would you suggest to include the error
handling in the interpolation subroutine, or to move it in the caller
subroutine? Thanks,
Best Regards
deltaquattro