We need to implement a sub that calls several other subs in a specific order. Each of these subs returns True if the sub was executed successfully and false otherwise.
If any sub returned false then we should stop calling other subs and return false. If all subs returned true then we need to return true.
B4X:
Sub DoSeveralTasks As Boolean
'call TaskA. If good continue to TaskB and so on...
End Sub
Sub TaskA(...) As Boolean
End Sub
Sub TaskB(...) As Boolean
End Sub
'more subs here. Each with a different name and a different number of parameters.
Please use the Spoiler tag in your answer:
It is of course very simple to implement this. The challenge is to write an elegant solution.
Yes, in my example I know.but what if I'm writing a class module or library where the Subs could be different depending on use? Then I wouldn't know ahead of time and a solution like#2 and #7 could come in handy.
Yes, in my example I know.but what if I'm writing a class module or library where the Subs could be different depending on use? Then I wouldn't know ahead of time and a solution like#2 and #7 could come in handy.
Yep, say that you are evaluating a file where each line can hold a different type of object. Depending on what you find on a line, you call a different sub. On the other hand, I would probably use a Select, just to be sure. I mean, the subs needs to be known at compile time anyway, even if you don't know which you are going to call.
Sorry for late contribution. I am on intergalactic spaceship 0.4 parsec from Earth; only just saw quiz.
Updated answer - should have read the question first. (Very dim lighting in my spaceship).
B4X:
sub GetResults
select case FALSE
case sub1(a,bc)
case sub2(x,y)
case sub3(p,q,r)
case ...
case else 'All cases must have been true
return TRUE
end select
'Must have encountered a false case
return FALSE
end sub
I think that's pretty readable. I always use this sort of construct rather than a whole bunch of if...then..else.
Sorry - readable but wrong. Needed to invert all Trues and Falses then it works correctly.
Otherwise this stops after the first sub returns True., should continue to first false one. I misread the question. It's a useful construct sometimes though.