For anyone who needs an example of persistence, something very simple but very useful follows:
Code: Select all
/*
Persistance function Demo1 (4 simple examples)
by Pablo César on February 13th, 2017
- First parameter: CodeBlock calling function with logical returns.
- Second parameter: Number of tries. Optonal and default value is 10 tries.
- You can use Persist function just without any conditional.
Just executing Persist function In the attempt to increase the chances
when they occur unsuccessful return.
- You can try to do it successively by asking the user if you want to
repeat the persistence.
- nHowMany var can be removed. It's just for ilustration propose only.
Not belongs to the persistance function.
- bQuestion vars can be done inside when calling Persist function.
- MsgInfo can be removed. It's just for ilustration propose only.
- First bQuestion is proposital (4, very few) for short persisting tries.
- Another logical bQuestion can be done for dates returns.
*/
#include <hmg.ch>
Function Main()
LOCAL bQuestion1:={|| Test_L() = .T.}
LOCAL bQuestion2:={|| Test_N() > 0}
LOCAL bQuestion3:={|| !Empty(Test_C())}
LOCAL bQuestion4:={|| ValType(Test_Nil())="U"}
PRIVATE nHowMany := 0 // This var can be removed
Persist({|| Test_L() = .T.})
While !Persist(bQuestion1,4) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo
If Persist(bQuestion1,4)
MsgInfo("Successful #1"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 4")
Else
MsgInfo("Unsucceeded #1","Upto 4")
Endif
If Persist(bQuestion2,10)
MsgInfo("Successful #2"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 10")
Else
MsgInfo("Unsucceeded #2","Upto 10")
Endif
If Persist(bQuestion3,20)
MsgInfo("Successful #3"+CRLF+"Tries: "+AllTrim(Str(nHowMany)),"Upto 20")
Else
MsgInfo("Unsucceeded #3","Upto 20")
Endif
If Persist(bQuestion4,2)
MsgInfo("Successful #4"+CRLF+"Try: " +AllTrim(Str(nHowMany)),"Upto 2")
Else
MsgInfo("Unsucceeded #4","Upto 2")
Endif
Return Nil
Function Persist(bCondition, nTryUpTo)
LOCAL nTries := 0, lRet := .F.
If !ValType(bCondition)="B"
MsgStop("First parameter must be a CodeBlock with logical return","First parameter is not a CodeBlock")
Return .F.
Endif
DEFAULT nTryUpTo := 10
nHowMany := 0 // This var can be removed
Do While nTries < nTryUpTo
nHowMany++ // This var can be removed
If Eval(bCondition)
lRet := .T.
nTries := nTryUpTo
Endif
nTries++
EndDo
Return lRet
Function Test_L()
STATIC nTimes_L := 0
LOCAL i, lRet := .F.
nTimes_L++
If nTimes_L = 8
lRet := .T.
Endif
Return lRet
Function Test_N()
STATIC nTimes_N := 0
LOCAL i, nRet := 0
nTimes_N++
If nTimes_N = 9
nRet := 1
Endif
Return nRet
Function Test_C()
STATIC nTimes_C := 0
LOCAL i, cRet := ""
nTimes_C++
If nTimes_C = 10
cRet := "Yes"
Endif
Return cRet
Function Test_Nil()
Return Nil // Always returns Nil (Not good return value for logical interest)
For example:You can also use Persist just without any logical returns.
Just executing Persist function in the attempt to increase the chances when it occurs unsuccessful return.
- Persist( bQuestion1, 4 )
- Persist( {|| Test_L() = .T.}, 4 )
For example:
- While !Persist( bQuestion1, 4 ) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo
- While !Persist( {|| Test_L() = .T.}, 4 ) .and. MsgYesNo("Do you want to try 4 times again ?")=.T. ; EndDo
Short, simple, practical, powerful and very useful. I guess.
I hope you enjoy.