Raising several run time errors in progress 4GL - error-handling

I want to test some AppServer error handling in progress. However, in order to test it I would like a run-time error with num-messages >= 2. How can you generate such multiple errors?
Here are some samples of what I am trying to do
IF ERROR-STATUS:ERROR THEN
LogToAppserver().
and
CATCH e AS Progress.Lang.Error :
LogToAppserver(e).
END CATCH.
where LogToAppserver looks like
METHOD PRIVATE VOID LogToAppserver( ):
DEFINE VARIABLE locNumErrors AS INTEGER NO-UNDO.
locNumErrors = ERROR-STATUS:NUM-MESSAGES.
DO WHILE locNumErrors > 0:
MESSAGE ERROR-STATUS:GET-MESSAGE (locNumErrors).
locNumErrors = locNumErrors - 1.
END.
RETURN.
END METHOD.
METHOD PRIVATE VOID LogToAppserver( INPUT iError AS Progress.Lang.Error ):
DEFINE VARIABLE locNumErrors AS INTEGER NO-UNDO.
locNumErrors = iError:NumMessages.
DO WHILE locNumErrors >0:
MESSAGE iError:GetMessage(locNumErrors ).
locNumErrors = locNumErrors - 1.
END.
RETURN.
END METHOD.

Try this code
DEF VAR h AS HANDLE NO-UNDO.
CREATE SERVER h.
h:CONNECT("") NO-ERROR.
DISPLAY ERROR-STATUS:num-messages.
It should give 2 messages.

Related

Code only producing 0 When I'm Trying to Concat Multiple Cells [duplicate]

How do I return a result from a function?
For example:
Public Function test() As Integer
return 1
End Function
This gives a compile error.
How do I make this function return an integer?
For non-object return types, you have to assign the value to the name of your function, like this:
Public Function test() As Integer
test = 1
End Function
Example usage:
Dim i As Integer
i = test()
If the function returns an Object type, then you must use the Set keyword like this:
Public Function testRange() As Range
Set testRange = Range("A1")
End Function
Example usage:
Dim r As Range
Set r = testRange()
Note that assigning a return value to the function name does not terminate the execution of your function. If you want to exit the function, then you need to explicitly say Exit Function. For example:
Function test(ByVal justReturnOne As Boolean) As Integer
If justReturnOne Then
test = 1
Exit Function
End If
'more code...
test = 2
End Function
Documentation: Function Statement
VBA functions treat the function name itself as a sort of variable. So instead of using a "return" statement, you would just say:
test = 1
Notice, though, that this does not break out of the function. Any code after this statement will also be executed. Thus, you can have many assignment statements that assign different values to test, and whatever the value is when you reach the end of the function will be the value returned.
Just setting the return value to the function name is still not exactly the same as the Java (or other) return statement, because in java, return exits the function, like this:
public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;
}
In VB, the exact equivalent takes two lines if you are not setting the return value at the end of your function. So, in VB the exact corollary would look like this:
Public Function test(ByVal x As Integer) As Integer
If x = 1 Then
test = 1 ' does not exit immediately. You must manually terminate...
Exit Function ' to exit
End If
' Still here? return 0 as default.
test = 0
' no need for an Exit Function because we're about to exit anyway.
End Function
Since this is the case, it's also nice to know that you can use the return variable like any other variable in the method. Like this:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End If
End Function
Or, the extreme example of how the return variable works (but not necessarily a good example of how you should actually code)—the one that will keep you up at night:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test > 0 Then
' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
' AND THE RESULT RESETTING THE RETURN VALUE.
test = test(test - 1)
End If
End Function

Case Statement VBA [duplicate]

How do I return a result from a function?
For example:
Public Function test() As Integer
return 1
End Function
This gives a compile error.
How do I make this function return an integer?
For non-object return types, you have to assign the value to the name of your function, like this:
Public Function test() As Integer
test = 1
End Function
Example usage:
Dim i As Integer
i = test()
If the function returns an Object type, then you must use the Set keyword like this:
Public Function testRange() As Range
Set testRange = Range("A1")
End Function
Example usage:
Dim r As Range
Set r = testRange()
Note that assigning a return value to the function name does not terminate the execution of your function. If you want to exit the function, then you need to explicitly say Exit Function. For example:
Function test(ByVal justReturnOne As Boolean) As Integer
If justReturnOne Then
test = 1
Exit Function
End If
'more code...
test = 2
End Function
Documentation: Function Statement
VBA functions treat the function name itself as a sort of variable. So instead of using a "return" statement, you would just say:
test = 1
Notice, though, that this does not break out of the function. Any code after this statement will also be executed. Thus, you can have many assignment statements that assign different values to test, and whatever the value is when you reach the end of the function will be the value returned.
Just setting the return value to the function name is still not exactly the same as the Java (or other) return statement, because in java, return exits the function, like this:
public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;
}
In VB, the exact equivalent takes two lines if you are not setting the return value at the end of your function. So, in VB the exact corollary would look like this:
Public Function test(ByVal x As Integer) As Integer
If x = 1 Then
test = 1 ' does not exit immediately. You must manually terminate...
Exit Function ' to exit
End If
' Still here? return 0 as default.
test = 0
' no need for an Exit Function because we're about to exit anyway.
End Function
Since this is the case, it's also nice to know that you can use the return variable like any other variable in the method. Like this:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End If
End Function
Or, the extreme example of how the return variable works (but not necessarily a good example of how you should actually code)—the one that will keep you up at night:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test > 0 Then
' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
' AND THE RESULT RESETTING THE RETURN VALUE.
test = test(test - 1)
End If
End Function

How do you return an error object from a function in VBA?

I have a function where the intention is to return either a string to signify that the function has completed and retrieved the required value or an error to indicate there has been an issue.
To see whether I could do this, I bashed together the following:
Private Function one()
Debug.Print TypeName(two(False))
End Function
Private Function two(b As Boolean) As Variant
Dim e As New ErrObject
If (b) Then
two = True
Else
two = e.number
End If
End Function
Now, this fails at two = e.number because you don't appear to be able to set an error number in this way - the syntax is incorrect.
I could use err.raise but then the intention is to pass the whole error object back as I can then have a custom number and fill in my own description etc...
How do you actually pass the err object back? Can you actually do it and is this the most effective way in achieving what I am setting out to do?
Thanks
You don't. Err is a globally-scoped Function from the standard library VBA.Information module, that returns an ErrObject instance.
the intention is to pass the whole error object back as I can then have a custom number and fill in my own description
You don't need to pass the whole error object back to throw custom errors.
Const ERR_SomethingBad = vbObjectError + 42
Err.Raise ERR_SomethingBad, "MyModule.MyProcedure", "Uh-oh"
Err is already in global scope - you don't need to pass error objects around... and in fact, you simply can't. Because the ErrObject class isn't creatable. This:
Dim e As ErrObject
Set e = New ErrObject
Throws run-time error 429 "ActiveX can't create object".
The very idea behind errors (and exceptions, if you're any familiar with languages that feature exceptions), is that they replace the old archaic "return status code" way, e.g.:
''' DON'T DO THIS, IT's 2017!!!
Public Function DoSomething(ByVal p1 As Double, ByRef outResult As Double) As Long
If p1 = 0 Then
DoSomething = 11 'return code for division by zero error
Exit Function
End If
outResult = 42 / p1
DoSomething = 0 'return code for OK - everything went well!
End Function
With this pattern every procedure is a Function that returns an error code; the actual return value is passed as a ByRef / out parameter, and the caller would have to do this before they can trust the result:
Dim result As Double
Dim e As Long
e = DoSomething(42, result)
If e = 0 Then
' happy path
Else
MsgBox Error$(e) ' error path
End If
The On Error statement streamlines the code by taking most error-handling concerns out of the "happy path".

pass parameter from loop value to dynamic function

I am making loop to make an array of Timers and give each timer a function
here's something like what i did:
dim timer(10) as Timer
for i = 0 to 5
timer(i) = new Timer
AddHandler timer(i).Tick, Function(senderX, eX) timerFunction(i)
next
i have this function:
Private Function timerFunction(ByVal timerNo As Integer)
MsgBox(timerNo)
End Function
but i am getting 6 as the value of timerNo for every timer i call with this:
timer(3).Start()
i outputs 6 even in i change the parameter to a number from 1 to 5
why is it diong that?
You have "closed over the loop variable". The value of timerNo is evaluated at the time the function is called which is always after the loop has completed, so the value of timerNo will always be 6.
You should have got a compiler warning: "BC42324 Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable."
To do this with your example...
Dim timer(10) As Timer
For i As Integer = 0 To 5
Dim j As Integer = i
timer(i) = New Timer
timer(i).Interval = 1000
timer(i).Enabled = True
AddHandler timer(i).Tick, Function(senderX, eX) timerFunction(j)
Next
Function timerFunction(timerNo As Integer) As String
MsgBox(timerNo)
Return timerNo.ToString
End Function
This way, a new instance of j is created for each iteration of the loop.

Initializing a static variable in VBA with non-default value

Static variables in VBA are simple enough:
Public Sub foo()
Static i As Integer
i = i + 1
Debug.Print i
End Sub
outputs (when called multiple times):
1
2
3
...
The problem is, VBA does not support initializing a variable on the same line as the declaration (not counting using : to put two lines on one):
Public Sub foo()
Dim i As Integer = 5 'won't compile!
Dim j As Integer
j = 5 'we have to do this instead
End Sub
This clashes with static variables:
Public Sub foo()
Static i As Integer 'we can't put an initial value here...
i = 5 'so this is how we'd usually initialize it, but...
i = i + 1
Debug.Print i
End Sub
You can probably see what happens - The very first thing the variable does every time foo is called is set itself back to 5. Output:
6
6
6
...
How can you initialize a static variable in VBA to a value other than its default? Or is this just VBA dropping the ball?
One way to do this if you want to keep the static semantics and not switch to a global is to sniff the default value and then set the initial condition:
Static i As Integer
if (i = 0) then i = 5
Safer alternative would perhaps be
Static i As Variant
if isempty(i) then i = 5
Or
Public Sub foo(optional init as boolean = false)
Static i As Integer
if init then
i = 5
exit sub
endif
You could probably also create a class with a default property and use class_initialize but that's probably a bit over-fussy.
I had the same issue in VB6, where it's exactly the same, and I like the Microsoft recommendation most:
Sub initstatic ()
Static Dummy, V, I As Integer, S As String
' The code in the following if statement will be executed only once:
If IsEmpty(Dummy) Then
' Initialize the dummy variant, so that IsEmpty returns FALSE for
' subsequent calls.
Dummy = 0
' Initialize the other static variables.
V = "Great"
I = 7
S = "Visual Basic"
End If
Print "V="; V, "I="; I, "S="; S
' Static variables will retain their values over calls when changed.
V = 7
I = I + 7
S = Right$(S, 5)
End Sub
I solved it as follows using a static boolean to indicate if you are entering the function for the first time. This logic should work for other situation as well, i think
Private Sub Validate_Date(TB as MSForms.TextBox)
Static Previous_Value as Date
Static Not_First_Time as Boolean
if Not_First_Time = False Then
Previous_Value = Now
Not_First_Time = True
endif
if IsDate(TB.Value) = False then TB.Value = Previous_Value
Previous_Value = TB.Value
End sub
The use of a Boolean to flag something as already initialized functions correctly in normal use but has an unexpected side effect when using the debugger. bIsInitialized is NOT reset to False when the VBA projuect is re-compiled. When the initialization code (or the constants the code uses) is changed changed, the thing being initialized will not be re-initialized while using the debugger.
One work-around is to set a breakpoint at the statement "If (Not bIsInitialized) Then" and add bIsInitialized as a watch variable. When the breakpoint is reached the first time, Click on the value and change it to false, remove the breakpoint and use F5 to continue. There may be a better work-around that uses something that is reliably reset by recompiling the project, but since the documentation for VBA says that the Boolean would be re-initialed after going out of context and all code stopping, there's no way to know if that behavior was version dependent. Not reinitializing the block of memory associated with the routine appears to be a performance optimization.
Static bIsInitialized as Boolean
Static something_time_consuming_to_initialize
If (Not bIsInitialized) Then
initialize something_time_consuming_to_initialize
bIsInitialized = True
End If