Forum Discussion
I have code in 3 functions
Function F1
Dim a
Dim b
a = 5
b = 6
If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F1 Function F2
Dim a
Dim b
a = 3
b = 0
If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F2 Function F3
Dim a
Dim b
a = 4
b = 7
If a = b Then Log.Error("A must be C!") Exit Function End If End Function ' F3
And I want to simplify this situation, f.e.
Function F1 Dim a Dim b a = 5 b = 6 execute(F4) ' <-- Error: script is terminated with an exception Function F2 Dim a Dim b a = 3 b = 0 eval (F4) ' <-- Error: script is terminated with an exception End Function ' F2 Function F3 Dim a Dim b a = 4 b = 7 execute(F4) ' <-- Error: script is terminated with an exception End Function ' F3 Function F4 Dim LocalVar_TmpTxt Dim LocalVar_CharEnter LocalVar_CharEnter = Chr(13) & Chr(10) LocalVar_TmpTxt = LocalVar_TmpTxt &_ "If a = b Then" & LocalVar_CharEnter & LocalVar_CharEnter LocalVar_TmpTxt = LocalVar_TmpTxt &_ "Log.Error(""A must be C!"")" & LocalVar_CharEnter & LocalVar_CharEnter LocalVar_TmpTxt = LocalVar_TmpTxt &_ "Exit Function" & LocalVar_CharEnter & LocalVar_CharEnter LocalVar_TmpTxt = LocalVar_TmpTxt &_ "End If" & LocalVar_CharEnter & LocalVar_CharEnter F4 = LocalVar_TmpTxt End Function ' F4
Why error: script is terminated with an exception ?
Hi Pakema
It's not really clear what you are trying to achieve here, but I have given you a basic solution to what I think you are trying to achieve below. It could be simplified further, but I don't want to lose you if you're just starting out!!
Basically each sub (F1, F2 and F3) will need to be executed separately, each will pass their a and b values to F4, which will determine whether they are equal and display a suitable message. The solution could easily be configured such that F4 is a function and returns a true or false value to each of the calling subs, which would then be able to deal with the result in their own way. It could be further simplified whereby only one sub is required and it could simply loop through a specified number of iterations, each time passing different values to F4, but without knowing what you are trying to achieve, I've kept it to the same basic format that you gave.
If you need further assistance, perhaps you could give us more specific details of exactly what it is you are trying to achieve.
sub F1 dim a dim b a = 5 b = 6 call F4(a, b) end sub sub F2 dim a dim b a = 3 b = 0 call F4(a, b) end sub function F3 dim a dim b a = 4 b = 7 call F4(a, b) end function sub F4(a, b) if a = b then log.message("A and B are the same") else Log.error("A must be B!") end if end sub
Regards
Stephen.
- Pakema10 years agoContributor
sbkeenan, I need a little another result
In first, I need many time call same code with same result,
In second, I need same exit function In 3 different function
By your idea I need write additional condition each function, Why?
I want once write macro code and calling it when I want without waiting result of code
- sbkeenan10 years agoFrequent Contributor
Hi Pakema
Unfortunately, I still don't fully understand what it is you are trying to do. Each of your three functions F1, F2 & F3 essentially do the same thing, they just use different values for the variables a and b. I've just replicated that in a way that works. It looks like your codein F4 is trying to build up a string of code that can be executed by each of the routines, but I can see no reason for you adopting this approach - what am I missing?
Do you know beforehand what the values will be? if so, are you looking to do something like this:
sub F5 dim i dim a, b dim numbers numbers = array(5,6,3,0,4,4) for i = 0 to ubound(numbers) step 2 a = numbers(i) b = numbers(i + 1) call F4(a, b) next end sub
This will essentially replace the F1, F2 & F3 routines as it contains all the values. I've changed the last 2 values so that you can see the final test passes. This example only tests 3 sets of values, but you can add as many sets of values to the array as is necessary.
Again, if this isn't what you're after, please give specific details rather than vague pointers. It's so much easier trying to solve a problem when we know exactly what it is, but hopefully what I've provided above will be of some assistance.
Finally, just looking at your supplied code, you seem to be using 'Exit Function' in each of the three routines F1, F2 and F3, but this isn't necessary since they are not doing any further processing, i.e. they will come to and end anyway. Your routines also only print out an error message to the log if the tests fail, butif a test passes, nothing will be output. My routine simply adds this functionality by stating that the values are equal if ther indeed are. If you don't require this, simply remove the else clause in my F4 routine.
Regards
Stephen.
- Pakema10 years agoContributor
sbkeenan, oh god
Ok, I try to explain what I want and what I mean
1. I have three function with parameters a, b, c
function f1 (a, b, c) end function ' f1 function f2 (a, b, c) end function ' f2 function f3 (a, b, c) end function ' f3
2. I what check params a, b, c
3, If one of them will be empty I must execute from function, and I check it with another function that has a return value
function f4 (a, b, c) Dim LocalVar_CheckArr Dim LocalVar_IterationCheckArr Dim LocalVar_TmpTxt LocalVar_CheckArr = Array(a, b, c) For LocalVar_IterationCheckArr = 0 To UBound(LocalVar_CheckArr ) LocalVar_TmpTxt = LocalVar_CheckArr(LocalVar_IterationCheckArr) If LocalVar_TmpTxt = "" or IsNull(LocalVar_TmpTxt ) or IsEmpty(LocalVar_TmpTxt ) Then f4 = False Exit function Next f4 = True end function ' f4
4. And in your idea I must dublicate condition THEREE times
function f1 (a, b, c) If Not f4(a, b, c) Then Log.Error("Some param was empty") Exit Function End If end function ' f1 function f2 (a, b, c) If Not f4(a, b, c) Then Log.Error("Some param was empty") Exit Function End If end function ' f2 function f3 (a, b, c) If Not f4(a, b, c) Then Log.Error("Some param was empty") Exit Function End If end function ' f3 function f4 (a, b, c) Dim LocalVar_CheckArr Dim LocalVar_IterationCheckArr Dim LocalVar_TmpTxt LocalVar_CheckArr = Array(a, b, c) For LocalVar_IterationCheckArr = 0 To UBound(LocalVar_CheckArr ) LocalVar_TmpTxt = LocalVar_CheckArr(LocalVar_IterationCheckArr) If LocalVar_TmpTxt = "" or IsNull(LocalVar_TmpTxt ) or IsEmpty(LocalVar_TmpTxt ) Then f4 = False Exit function Next f4 = True end function ' f4
5. But my question is - What I must wrote and do some other that I write just one string?
function f1 (a, b, c) Execute (f5) end function ' f1 function f2 (a, b, c) Execute (f5) end function ' f2 function f3 (a, b, c) Execute (f5) end function ' f3 function f4 (a, b, c) Dim LocalVar_CheckArr Dim LocalVar_IterationCheckArr Dim LocalVar_TmpTxt LocalVar_CheckArr = Array(a, b, c) For LocalVar_IterationCheckArr = 0 To UBound(LocalVar_CheckArr ) LocalVar_TmpTxt = LocalVar_CheckArr(LocalVar_IterationCheckArr) If LocalVar_TmpTxt = "" or IsNull(LocalVar_TmpTxt ) or IsEmpty(LocalVar_TmpTxt ) Then f4 = False Exit function Next f4 = True end function ' f4 function f5 dim LocalVar_TmpTxt dim LocalVar_CharEnter LocalVar_CharEnter = Chr(13) & Chr(10) LocalVar_TmpTxt = LocalVar_TmpTxt &_ "If Not f4(a, b, c) Then" & LocalVar_CharEnter &_ "Log.Error(""Some param was empty"")" & LocalVar_CharEnter &_ "Exit Function" & LocalVar_CharEnter &_ "End If" f5 = LocalVar_TmpTxt end function ' f5
Related Content
- 8 years ago
Recent Discussions
- 15 hours ago
- 17 hours ago
- 19 hours ago