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. switch statement

switch statement

Scheduled Pinned Locked Moved C#
csharpquestionannouncement
5 Posts 4 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
    codemunkeh
    wrote on last edited by
    #1

    Upgraded some code from VB to C#.

    Select Case whatever
    Case x To y
    ' mmhmm
    Case Is > z
    ' yeesh
    Case Else
    ' ??
    End Select

    So I changed this to:

    switch (whatever) {
    case x to y:
    // mmhmm
    break;
    case is > z:
    // yeesh
    break;
    default:
    // ??
    break;
    }

    But, obviously To and Is don't convert. What do I use instead? Do I need to replace it all with if (x >= whatever >= y) [update] Forgot to put the closing brace in the code sample.


    Ninja (the Nerd)
    Confused? You will be...

    J S 2 Replies Last reply
    0
    • C codemunkeh

      Upgraded some code from VB to C#.

      Select Case whatever
      Case x To y
      ' mmhmm
      Case Is > z
      ' yeesh
      Case Else
      ' ??
      End Select

      So I changed this to:

      switch (whatever) {
      case x to y:
      // mmhmm
      break;
      case is > z:
      // yeesh
      break;
      default:
      // ??
      break;
      }

      But, obviously To and Is don't convert. What do I use instead? Do I need to replace it all with if (x >= whatever >= y) [update] Forgot to put the closing brace in the code sample.


      Ninja (the Nerd)
      Confused? You will be...

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Ninja-the-Nerd wrote:

      But, obviously To and Is don't convert. What do I use instead? Do I need to replace it all with if (x >= whatever >= y)

      In this case, with only 3 options I would say this is your best bet. if(whatever > x && whatever < y) { //mmhmm } else if(whatever > z) { // yeesh } else { // ?? }

      --- How to get answers to your questions[^]

      C 1 Reply Last reply
      0
      • J J4amieC

        Ninja-the-Nerd wrote:

        But, obviously To and Is don't convert. What do I use instead? Do I need to replace it all with if (x >= whatever >= y)

        In this case, with only 3 options I would say this is your best bet. if(whatever > x && whatever < y) { //mmhmm } else if(whatever > z) { // yeesh } else { // ?? }

        --- How to get answers to your questions[^]

        C Offline
        C Offline
        codemunkeh
        wrote on last edited by
        #3

        So it can't be done then? Nuts. Incidentally how do you convert If searchString Like "p#" Then I've written a strLeft function to do what VB6 did. It works well enough, I could change it and check for if (functionClass.strLeft(searchString,4) == "page"). Thanks btw.


        Ninja (the Nerd)
        Confused? You will be...

        U 1 Reply Last reply
        0
        • C codemunkeh

          So it can't be done then? Nuts. Incidentally how do you convert If searchString Like "p#" Then I've written a strLeft function to do what VB6 did. It works well enough, I could change it and check for if (functionClass.strLeft(searchString,4) == "page"). Thanks btw.


          Ninja (the Nerd)
          Confused? You will be...

          U Offline
          U Offline
          Urs Enzler
          wrote on last edited by
          #4

          You can use searchString.StartsWith(p)

          -^-^-^-^-^- no risk no funk ................... please vote ------>

          1 Reply Last reply
          0
          • C codemunkeh

            Upgraded some code from VB to C#.

            Select Case whatever
            Case x To y
            ' mmhmm
            Case Is > z
            ' yeesh
            Case Else
            ' ??
            End Select

            So I changed this to:

            switch (whatever) {
            case x to y:
            // mmhmm
            break;
            case is > z:
            // yeesh
            break;
            default:
            // ??
            break;
            }

            But, obviously To and Is don't convert. What do I use instead? Do I need to replace it all with if (x >= whatever >= y) [update] Forgot to put the closing brace in the code sample.


            Ninja (the Nerd)
            Confused? You will be...

            S Offline
            S Offline
            Scott Dorman
            wrote on last edited by
            #5

            C# doesn't have the same concept behind switch statements as VB. It can be done, but you would need to list all of the values between "x" and "y". The "is > z" can't be represented by a case, however. Your best bet would be to do either rework your logic so you can use the switch, which would probably mean defining more specific cases and using the default case to handle the "is > z" condition, or to use if/else blocks.

            Scott.


            —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

            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