Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Exit Nested Methods

How to Exit Nested Methods

Scheduled Pinned Locked Moved C#
tutorialquestionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coder05
    wrote on last edited by
    #1

    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

    D R 2 Replies Last reply
    0
    • C coder05

      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

      D Offline
      D Offline
      DigitalKing
      wrote on last edited by
      #2

      void 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;
      }

      1 Reply Last reply
      0
      • C coder05

        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

        R Offline
        R Offline
        RicoH
        wrote on last edited by
        #3

        You 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups