How to Exit Nested Methods
-
How do I exit multiple, nested, methods when a condition is true? For example, an event is invoked, then when x = 0, stop activity
void event1(...) { //this event is called first method1() //more stuff } public void method1() { //some stuff method2(); //more stuff } public void method2() { //some stuff method3(); //more stuff } public void method3() { //some stuff if (x == 0) { //now exit //???????? } //more stuff }
If x = 0, the "more stuff" should never be done. I know I can exit each separately, but I would have to test x each time. Thanks for any help. bigiorno -
How do I exit multiple, nested, methods when a condition is true? For example, an event is invoked, then when x = 0, stop activity
void event1(...) { //this event is called first method1() //more stuff } public void method1() { //some stuff method2(); //more stuff } public void method2() { //some stuff method3(); //more stuff } public void method3() { //some stuff if (x == 0) { //now exit //???????? } //more stuff }
If x = 0, the "more stuff" should never be done. I know I can exit each separately, but I would have to test x each time. Thanks for any help. bigiornovoid event1(...) {
//this event is called first
if (method1())
{
//more stuff
}
}public bool method1() {
//some stuff
if (!method2()) return false;
//more stuff
return true;
}public bool method2() {
//some stuff
if (!method3()) return false;
//more stuff
return true;
}public bool method3() {
//some stuff
if (x == 0) {
return false;
}
//more stuff
return true;
} -
How do I exit multiple, nested, methods when a condition is true? For example, an event is invoked, then when x = 0, stop activity
void event1(...) { //this event is called first method1() //more stuff } public void method1() { //some stuff method2(); //more stuff } public void method2() { //some stuff method3(); //more stuff } public void method3() { //some stuff if (x == 0) { //now exit //???????? } //more stuff }
If x = 0, the "more stuff" should never be done. I know I can exit each separately, but I would have to test x each time. Thanks for any help. bigiornoYou could use a return code instead of using 'void' everywhere. The return code would indicate if the method succeeded... Your code would become:
void event1(...) { //this event is called first if (method1()) { //more stuff } } public bool method1() { //some stuff if (method2()) { //more stuff return true; } return false; } public bool method2() { //some stuff if (method3()) { //more stuff return true; } return false; } public bool method3() { //some stuff if (x == 0) { //now exit return false; } //more stuff return true; }
another way would be to use exceptions which would make your code look like:void event1(...) { try { //this event is called first method1(); //more stuff } catch (Exception e) { // something went wrong... } } public void method1() { //some stuff method2(); //more stuff } public void method2() { //some stuff method3(); //more stuff } public void method3() { //some stuff if (x == 0) { //now exit // create an exception Exception e; // blablabla... throw e; } //more stuff }
Hope this helps... RicoH Don't think you are, know you are... custom hardware & software - olloc.be