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. swicth & case?

swicth & case?

Scheduled Pinned Locked Moved C#
questioncsharpc++
11 Posts 5 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.
  • D dec82

    i need to convert C++ code into C#. Then the C++ in the form: number=1 switch(number) { case 1: //do something number++ case 2: //dosomething number++; case 3: if ( ) { //do something number++; } else { number= 100; } case 4: ............ case 120: //do something break; } what is the easiest way to convert these code ? thanks

    X Offline
    X Offline
    Xmen Real
    wrote on last edited by
    #2

    this will be same in C# but I would like to prefer if statement.

    TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

    ----------------------------------------------- 128 bit encrypted signature, crack if you can

    1 Reply Last reply
    0
    • D dec82

      i need to convert C++ code into C#. Then the C++ in the form: number=1 switch(number) { case 1: //do something number++ case 2: //dosomething number++; case 3: if ( ) { //do something number++; } else { number= 100; } case 4: ............ case 120: //do something break; } what is the easiest way to convert these code ? thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #3

      Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:

      case 76:
      label76:
      ...
      goto label77;
      case 77:
      label77:
      ...
      goto label78;

      This adds two lines per case instead of one, but executes the switch only once. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      P D 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:

        case 76:
        label76:
        ...
        goto label77;
        case 77:
        label77:
        ...
        goto label78;

        This adds two lines per case instead of one, but executes the switch only once. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #4

        Luc Pattyn wrote:

        goto label77;

        Why not goto case 77; ?

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, you can't fall through from one case to the next (except for empty cases), there must be an explicit change of program flow (break, return, throw, goto, ...) your C++ code looks like a sequence of operations with a selectable start. the easiest way IMO to get that done in C# is by putting everything in a for( ; ; ) {} and using a continue after each number++, plus of course providing an exit somehow. However doing so there will be a performance hit since now the switch dispatching code gets executed over and over. The ugly but high-performance alternative uses goto and labels; something like:

          case 76:
          label76:
          ...
          goto label77;
          case 77:
          label77:
          ...
          goto label78;

          This adds two lines per case instead of one, but executes the switch only once. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          D Offline
          D Offline
          dec82
          wrote on last edited by
          #5

          Is label needed? Can i do this way: case 1: // do something goto case 2; case 2: //do something goto case 3 ....... thanks!

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            Luc Pattyn wrote:

            goto label77;

            Why not goto case 77; ?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            PIEBALDconsult wrote:

            goto case 77

            O great, didn't know that. That will get me going places. Thanks. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            P 1 Reply Last reply
            0
            • L Luc Pattyn

              PIEBALDconsult wrote:

              goto case 77

              O great, didn't know that. That will get me going places. Thanks. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #7

              Glad to be of service.

              1 Reply Last reply
              0
              • D dec82

                Is label needed? Can i do this way: case 1: // do something goto case 2; case 2: //do something goto case 3 ....... thanks!

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #8

                Why does my skin crawl whenever I see a goto in code X|. I guess it is legitimate in this structure but I would never encourage the use of goto's, the potential for disasterous spaghetti dumps is too high.

                Never underestimate the power of human stupidity RAH

                P 1 Reply Last reply
                0
                • M Mycroft Holmes

                  Why does my skin crawl whenever I see a goto in code X|. I guess it is legitimate in this structure but I would never encourage the use of goto's, the potential for disasterous spaghetti dumps is too high.

                  Never underestimate the power of human stupidity RAH

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #9

                  # define proceedto goto :-D

                  D 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    # define proceedto goto :-D

                    D Offline
                    D Offline
                    dec82
                    wrote on last edited by
                    #10

                    Could you explain more? thanks

                    P 1 Reply Last reply
                    0
                    • D dec82

                      Could you explain more? thanks

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #11

                      I was merely demonstrating that (by using the C pre-processor) one could use goto without actually seeing the goto keyword. It was a joke.

                      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