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. visual C# 2005 exiting

visual C# 2005 exiting

Scheduled Pinned Locked Moved C#
csharphelptutorial
56 Posts 9 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.
  • L led mike

    xxmikexx wrote:

    this is hard to explain but there is no other way i can think of doing this i know in visual basic there was something

    look at the C# language documentation for break and return however I strongly suggest you get a beginners book on C# or stick to Visual Basically Garbage.

    led mike

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #6

    I shake my head in dismay and sadness, then realize that at least some of us will have job security for a long time to come from the sounds of it.


    only two letters away from being an asset

    X 1 Reply Last reply
    0
    • N Not Active

      I shake my head in dismay and sadness, then realize that at least some of us will have job security for a long time to come from the sounds of it.


      only two letters away from being an asset

      X Offline
      X Offline
      xxmikexx
      wrote on last edited by
      #7

      also i shake my head that there are aggorant people like you out there you didnt have to help me out and there was no need for that i am just trying to learn and not fail... i have a deadline... but i thank the first guy for the help:confused:

      N 1 Reply Last reply
      0
      • X xxmikexx

        also i shake my head that there are aggorant people like you out there you didnt have to help me out and there was no need for that i am just trying to learn and not fail... i have a deadline... but i thank the first guy for the help:confused:

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #8

        xxmikexx wrote:

        i am just trying to learn and not fail... i have a deadline

        What did you do with the rest of the semester then?


        only two letters away from being an asset

        X L 2 Replies Last reply
        0
        • X xxmikexx

          i need help with a quick program i am making, how would i exit a block of code in an if statement for example private void button1_Click(object sender, EventArgs e) { code to be run here if (x ==5) { run more code exit this block } more code here } this is hard to explain but there is no other way i can think of doing this i know in visual basic there was something called exit sub (or something similiar been a while) that would leave it thanks in advance

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #9

          Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle. You should use a proper combination of if, elseif, and else blocks to properly ensure program flow execution. (When I was a TA I took points off for liberal use of return)

          Need a C# Consultant? I'm available.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

          X N P L 4 Replies Last reply
          0
          • N Not Active

            xxmikexx wrote:

            i am just trying to learn and not fail... i have a deadline

            What did you do with the rest of the semester then?


            only two letters away from being an asset

            X Offline
            X Offline
            xxmikexx
            wrote on last edited by
            #10

            i have been learning and our semester just started...

            L 1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle. You should use a proper combination of if, elseif, and else blocks to properly ensure program flow execution. (When I was a TA I took points off for liberal use of return)

              Need a C# Consultant? I'm available.
              Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

              X Offline
              X Offline
              xxmikexx
              wrote on last edited by
              #11

              ok thanks but the reason i need it is if the first block of if statements is executed it will also execute the rest i want each one to trigger at a different time, i am making a program were you click a spot on the screen and it sends waves out i have it working but it just does it really quick if i use this it will wok for sure thanks for the advice tho

              1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle. You should use a proper combination of if, elseif, and else blocks to properly ensure program flow execution. (When I was a TA I took points off for liberal use of return)

                Need a C# Consultant? I'm available.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #12

                Ennis Ray Lynch, Jr. wrote:

                Return is not allowed

                Not allowed, or not recommended? Two different things. I don't know of any language that places a restriction on the number of return statements that can be used in a block of code. However, yes, good design principles dictate that you should have as few exit points as possible. The original question only asked what the equivalent of VB's exit sub was, not how it should be used. Both Mike and I answered that direct question. Saying "Don't listen to anyone else" is a bit over the top.


                only two letters away from being an asset

                E 1 Reply Last reply
                0
                • X xxmikexx

                  i need help with a quick program i am making, how would i exit a block of code in an if statement for example private void button1_Click(object sender, EventArgs e) { code to be run here if (x ==5) { run more code exit this block } more code here } this is hard to explain but there is no other way i can think of doing this i know in visual basic there was something called exit sub (or something similiar been a while) that would leave it thanks in advance

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #13

                  xxmikexx wrote:

                  private void button1_Click(object sender, EventArgs e) { code to be run here if (x ==5) { run more code exit this block } more code here }

                  From the look of your code, you don't need what you're asking for. A properly written if block is all it takes. You just about never need to use the "exit early" statements if the logic is laid out properly. Rewritten, you code translates to this:

                  private void button1_Click(object sender, EventArgs e)
                  {
                  // code to be run here

                  if (x ==5)
                  {
                      // run more code
                      // once this code is done, the 'else' statements
                      // are skipped and execution picks up after the
                      // last brace in the 'if' block.
                  }
                  else
                  {
                      // more code here
                  }
                  
                  // Execution picks up here after either of the 'if'
                  // statements blocks are executed.
                  

                  }

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  X 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle. You should use a proper combination of if, elseif, and else blocks to properly ensure program flow execution. (When I was a TA I took points off for liberal use of return)

                    Need a C# Consultant? I'm available.
                    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                    P Offline
                    P Offline
                    pmarfleet
                    wrote on last edited by
                    #14

                    Ennis Ray Lynch, Jr. wrote:

                    Return is not allowed inside of methods more than once

                    This isn't true. The C# language specification allows methods to contain multiple return statements. The question to ask is whether a method should contain multiple return statements. The Microsoft Code Complete book makes some good arguments for and against this practice. There are definitely cases when multiple return statements make for greater readability and reduced complexity.

                    Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      xxmikexx wrote:

                      private void button1_Click(object sender, EventArgs e) { code to be run here if (x ==5) { run more code exit this block } more code here }

                      From the look of your code, you don't need what you're asking for. A properly written if block is all it takes. You just about never need to use the "exit early" statements if the logic is laid out properly. Rewritten, you code translates to this:

                      private void button1_Click(object sender, EventArgs e)
                      {
                      // code to be run here

                      if (x ==5)
                      {
                          // run more code
                          // once this code is done, the 'else' statements
                          // are skipped and execution picks up after the
                          // last brace in the 'if' block.
                      }
                      else
                      {
                          // more code here
                      }
                      
                      // Execution picks up here after either of the 'if'
                      // statements blocks are executed.
                      

                      }

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      X Offline
                      X Offline
                      xxmikexx
                      wrote on last edited by
                      #15

                      thanks for that it is really hard to explain what i mean tho, i know about that but since it is a timer once it is executed it will set off all of the others that is what i want it to do but i want to go through one if statement each time the timer is used. i am making a program with a group of 16 buttons and when they click one it changes the colors of those around it and i want to send out a wave effect so the only practical way i could think of was finding something similiar to that break thing other wise all of them will go off i just dont really know how to explain what i need thanks for the help tho

                      D 1 Reply Last reply
                      0
                      • X xxmikexx

                        i have been learning and our semester just started...

                        L Offline
                        L Offline
                        led mike
                        wrote on last edited by
                        #16

                        xxmikexx wrote:

                        i have been learning

                        I guess you are not a quick learner. You must be the first one. what happens if you get better at programming, you add more x's to your name?

                        led mike

                        X 1 Reply Last reply
                        0
                        • E Ennis Ray Lynch Jr

                          Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle. You should use a proper combination of if, elseif, and else blocks to properly ensure program flow execution. (When I was a TA I took points off for liberal use of return)

                          Need a C# Consultant? I'm available.
                          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #17

                          Ennis Ray Lynch, Jr. wrote:

                          Return is not allowed inside of methods more than once and should not be used by any method that does not have a functional result. Doing so is a poor programming practice that violates the one-in, one-out principle.

                          Yeah, that's what I thought also until I read Implementation Patterns by Kent Beck: Back in the old days of programming, a commandment was issued: each routine shall have a single entry and a single exit. This was to prevent the confusion possible when jumping into and out of many locations in the same routine. It made good sense when applied to FORTRAN or assembly language programs written with lots of global data where even understanding which statements were executed was hard work. In Java, with small methods and mostly local data, it is needlessly conservative. However, this bit of programming folklore, thoughtlessly obeyed, prevents the use of guard clauses. Guard clauses are particularly useful when there are multiple conditions: [Code example snipped of nested if statements with a single return] Nested conditionals breed defects. The guard clause version of the same code notes the prerequisites to processing a request without complex control structures: [Code example snipped of flat if statements each with a return ] Keep in mind there is more context to his segment on the Guard Clause. If you are interested I highly recommend the book.

                          All truth passes through three stages. First, it is ridiculed. Second, it is violently opposed. Third, it is accepted as being self-evident.

                          Arthur Schopenhauer - German philosopher (1788 - 1860)

                          E 1 Reply Last reply
                          0
                          • L led mike

                            xxmikexx wrote:

                            i have been learning

                            I guess you are not a quick learner. You must be the first one. what happens if you get better at programming, you add more x's to your name?

                            led mike

                            X Offline
                            X Offline
                            xxmikexx
                            wrote on last edited by
                            #18

                            guess not i am damn sure you didnt learn how to program for yourself in 3 days i have spent alot of time trying to find websites were i can learn that are not blocked this is a new thing i can not do and all i know is i am definetly not coming back here for help any more a few people on here are very helpfull but just a couple people are incredibly annoying, i dont see what i did wrong i tried looking it up for my self then i figured i would ask for help and the reason i have the X's in my name is because i hate numbers in my name also this is the same name i have had on all the websites i go to one user name thats it so....

                            modified on Wednesday, March 19, 2008 5:03 PM

                            1 Reply Last reply
                            0
                            • X xxmikexx

                              i need help with a quick program i am making, how would i exit a block of code in an if statement for example private void button1_Click(object sender, EventArgs e) { code to be run here if (x ==5) { run more code exit this block } more code here } this is hard to explain but there is no other way i can think of doing this i know in visual basic there was something called exit sub (or something similiar been a while) that would leave it thanks in advance

                              E Offline
                              E Offline
                              engsrini
                              wrote on last edited by
                              #19

                              int x = 5; if (x == 5) { Debug.WriteLine("more code to runs here1"); Debug.WriteLine("more code to runs here2"); if(done) return; else { Debug.WriteLine("more code to runs here3"); Debug.WriteLine("more code to runs here4"); Debug.WriteLine("more code to runs here5"); } } Debug.WriteLine("more code to runs here6"); Debug.WriteLine("more code to runs here7"); Debug.WriteLine("more code to runs here8"); does it helps? Srini

                              X 2 Replies Last reply
                              0
                              • E engsrini

                                int x = 5; if (x == 5) { Debug.WriteLine("more code to runs here1"); Debug.WriteLine("more code to runs here2"); if(done) return; else { Debug.WriteLine("more code to runs here3"); Debug.WriteLine("more code to runs here4"); Debug.WriteLine("more code to runs here5"); } } Debug.WriteLine("more code to runs here6"); Debug.WriteLine("more code to runs here7"); Debug.WriteLine("more code to runs here8"); does it helps? Srini

                                X Offline
                                X Offline
                                xxmikexx
                                wrote on last edited by
                                #20

                                kinda i am using visual C# not the console one the one were you can place buttons and stuff they are similiar but different i cant test this till i am at school tomorrow when it returns where does it go to? this seems to help tho thanks alot if it just leaves the whole if statement that would be great

                                N E 2 Replies Last reply
                                0
                                • N Not Active

                                  xxmikexx wrote:

                                  i am just trying to learn and not fail... i have a deadline

                                  What did you do with the rest of the semester then?


                                  only two letters away from being an asset

                                  L Offline
                                  L Offline
                                  led mike
                                  wrote on last edited by
                                  #21

                                  Wow you really got him fired up. I almost suffocated trying to read his post to me without taking a breath.

                                  led mike

                                  X N 2 Replies Last reply
                                  0
                                  • L led mike

                                    Wow you really got him fired up. I almost suffocated trying to read his post to me without taking a breath.

                                    led mike

                                    X Offline
                                    X Offline
                                    xxmikexx
                                    wrote on last edited by
                                    #22

                                    ... do you have a problem reading in your head?

                                    1 Reply Last reply
                                    0
                                    • X xxmikexx

                                      kinda i am using visual C# not the console one the one were you can place buttons and stuff they are similiar but different i cant test this till i am at school tomorrow when it returns where does it go to? this seems to help tho thanks alot if it just leaves the whole if statement that would be great

                                      N Offline
                                      N Offline
                                      Not Active
                                      wrote on last edited by
                                      #23

                                      The first thing you should do when you get to school tomorrow is visit the admissions office and drop the class, then change your major. The example he gave wasn't using a console window, it was using the debug window. Notice the difference, I'll spell it slowly, D e b u g and C o n s o l e. And BTW, the "the one were you can place buttons and stuff" is referred to as a Windows application. Your friends who graduate can tell you all about it.


                                      only two letters away from being an asset

                                      X 2 Replies Last reply
                                      0
                                      • L led mike

                                        Wow you really got him fired up. I almost suffocated trying to read his post to me without taking a breath.

                                        led mike

                                        N Offline
                                        N Offline
                                        Not Active
                                        wrote on last edited by
                                        #24

                                        Guess he is struggling in his writing courses also. Punctuation is overrated anyway. :-D


                                        only two letters away from being an asset

                                        L X 2 Replies Last reply
                                        0
                                        • N Not Active

                                          The first thing you should do when you get to school tomorrow is visit the admissions office and drop the class, then change your major. The example he gave wasn't using a console window, it was using the debug window. Notice the difference, I'll spell it slowly, D e b u g and C o n s o l e. And BTW, the "the one were you can place buttons and stuff" is referred to as a Windows application. Your friends who graduate can tell you all about it.


                                          only two letters away from being an asset

                                          X Offline
                                          X Offline
                                          xxmikexx
                                          wrote on last edited by
                                          #25

                                          wow just wow how am i supposed to know that everything at school s blocked i have been programming for 3 days sorry i am not some master programmer in the 3 days since i have started everything i have learned is from code snippets... just please please do me a favor and dont comment on this thread if you are not going to help thats fine dont insult me... better yet just go die

                                          N 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