Erel B4X founder Staff member Licensed User Longtime User Oct 13, 2014 #1 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. Code: 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. Last edited: Oct 13, 2014
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. Code: 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.
LucaMs Expert Licensed User Longtime User Oct 13, 2014 #21 James Chamblin said: 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. Click to expand... In the moment in which you instantiate that class you know the names of your routines Upvote 0
James Chamblin said: 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. Click to expand... In the moment in which you instantiate that class you know the names of your routines
J James Chamblin Active Member Licensed User Longtime User Oct 13, 2014 #22 LucaMs said: In the moment in which you instantiate that class you know the names of your routines Click to expand... That is true; however the class does not know. How can I write a class that calls routines that I don't know when the class is written? Upvote 0
LucaMs said: In the moment in which you instantiate that class you know the names of your routines Click to expand... That is true; however the class does not know. How can I write a class that calls routines that I don't know when the class is written?
LucaMs Expert Licensed User Longtime User Oct 13, 2014 #23 James Chamblin said: That is true; however the class does not know. How can I write a class that calls routines that I don't know when the class is written? Click to expand... You don't need that kind of "service" into (from) your class, just use #3 Upvote 0
James Chamblin said: That is true; however the class does not know. How can I write a class that calls routines that I don't know when the class is written? Click to expand... You don't need that kind of "service" into (from) your class, just use #3
T Troberg Well-Known Member Licensed User Longtime User Oct 13, 2014 #24 James Chamblin said: 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. Click to expand... 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. Upvote 0
James Chamblin said: 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. Click to expand... 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.
LucaMs Expert Licensed User Longtime User Oct 13, 2014 #25 LucaMs said: Can you lock the spoiler up to a deadline? Click to expand... Forget it: I can read the html source Well, until there are no prizes, we can trust the honesty of all the members of the site Upvote 0
LucaMs said: Can you lock the spoiler up to a deadline? Click to expand... Forget it: I can read the html source Well, until there are no prizes, we can trust the honesty of all the members of the site
Erel B4X founder Staff member Licensed User Longtime User Oct 13, 2014 #26 LucaMs said: Can you lock the spoiler up to a deadline? Click to expand... No. Upvote 0
Derek Johnson Active Member Licensed User Longtime User Jun 13, 2015 #27 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). Spoiler: My answer Code: 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. Derek Last edited: Jun 13, 2015 Upvote 0
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). Spoiler: My answer Code: 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. Derek
Derek Johnson Active Member Licensed User Longtime User Jun 13, 2015 #28 Derek Johnson said: Sorry for late contribution. I am on intergalactic spaceship 0.4 parsec from Earth; only just saw quiz. Spoiler: My answer Code: sub GetResults select case true case sub1(a,bc) case sub2(x,y) case sub3(p,q,r) case ... case else return false end select return true end sub I think that's pretty readable. I always use this sort of construct rather than a whole bunch of if...then..else. Derek Click to expand... 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. Last edited: Jun 13, 2015 Upvote 0
Derek Johnson said: Sorry for late contribution. I am on intergalactic spaceship 0.4 parsec from Earth; only just saw quiz. Spoiler: My answer Code: sub GetResults select case true case sub1(a,bc) case sub2(x,y) case sub3(p,q,r) case ... case else return false end select return true end sub I think that's pretty readable. I always use this sort of construct rather than a whole bunch of if...then..else. Derek Click to expand... 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.