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. Info for switch statements

Info for switch statements

Scheduled Pinned Locked Moved C#
questioncsharp
5 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.
  • O Offline
    O Offline
    Omega501
    wrote on last edited by
    #1

    I was talking to a VB programmer a few days ago, and got to talking about switch/select statements, and how in VB you can do things like Case >10 but you can't in C#. Has anyone found a way around this at all? or do you have to fall back on multiple if statements?? or even worse, itemise every possible option that could come through the switch? Also, am I looking at this the wrong way - if so, what is the proper way to handle these kinds of situations?

    J 1 Reply Last reply
    0
    • O Omega501

      I was talking to a VB programmer a few days ago, and got to talking about switch/select statements, and how in VB you can do things like Case >10 but you can't in C#. Has anyone found a way around this at all? or do you have to fall back on multiple if statements?? or even worse, itemise every possible option that could come through the switch? Also, am I looking at this the wrong way - if so, what is the proper way to handle these kinds of situations?

      J Offline
      J Offline
      Julian Bucknall MSFT
      wrote on last edited by
      #2

      No, I'm afraid you can't do that with C#. The expressions in the case labels must be constant since they're evaluated at compile-time (for speed at run-time). To do what you want to do, you'd use a structure like

      if (firstCondition) {
      // do stuff
      }
      else if (secondCondition) {
      // do some other stuff
      }
      else {
      // otherwise if all fails do this
      }

      Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

      O 1 Reply Last reply
      0
      • J Julian Bucknall MSFT

        No, I'm afraid you can't do that with C#. The expressions in the case labels must be constant since they're evaluated at compile-time (for speed at run-time). To do what you want to do, you'd use a structure like

        if (firstCondition) {
        // do stuff
        }
        else if (secondCondition) {
        // do some other stuff
        }
        else {
        // otherwise if all fails do this
        }

        Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

        O Offline
        O Offline
        Omega501
        wrote on last edited by
        #3

        That works if you only have 3 or so conditions, but what happens if you have 30 conditions? How can you allow for so many responses without using a series of if-elseif statements? This is hypothetical really, but there are some situations where either through bad design, poor experience or even bad luck that you end up needing something like this

        J 1 Reply Last reply
        0
        • O Omega501

          That works if you only have 3 or so conditions, but what happens if you have 30 conditions? How can you allow for so many responses without using a series of if-elseif statements? This is hypothetical really, but there are some situations where either through bad design, poor experience or even bad luck that you end up needing something like this

          J Offline
          J Offline
          Julian Bucknall MSFT
          wrote on last edited by
          #4

          Then you have two choices, I suppose: either write out the 30 or so if-elseif lines, or, you take a step back and consider how to refactor the code into something cleaner. The switch statement won't help you, I'm afraid. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

          A 1 Reply Last reply
          0
          • J Julian Bucknall MSFT

            Then you have two choices, I suppose: either write out the 30 or so if-elseif lines, or, you take a step back and consider how to refactor the code into something cleaner. The switch statement won't help you, I'm afraid. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            The point is if you're using case >10 <30, case >30, default...then your code won't need many cases and if/else works fine. If you have constants, e.g. some sort of parser then const cases make sense.

            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