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/Case Question

Switch/Case Question

Scheduled Pinned Locked Moved C#
helpquestionworkspace
6 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.
  • F Offline
    F Offline
    ffowler
    wrote on last edited by
    #1

    Good day all! I believe I have a rather simple question but I can not seem to work it out using a Switch statement. I have a small function that checks the time and if the time is one of the parameters set (a total of 6 time points), then I wish to peform a duty. Instead of using an if statement for all of the time points I wanted to add 3 time points to one case and 3 to another and do nothing in the default. I cannot compile my function while I have the case statements setup in this manner: case "10:00", "11:20", "13:30": Do something break; case "17:00", "19:20", "23:30": Do something else break; default break; Can anyone pinpoint my syntax error with the cases I have defined above. When I used VB, this syntax worked fine with the multiple points but it doesn't now. Any pointers would be greatly appreciated.

    J 1 Reply Last reply
    0
    • F ffowler

      Good day all! I believe I have a rather simple question but I can not seem to work it out using a Switch statement. I have a small function that checks the time and if the time is one of the parameters set (a total of 6 time points), then I wish to peform a duty. Instead of using an if statement for all of the time points I wanted to add 3 time points to one case and 3 to another and do nothing in the default. I cannot compile my function while I have the case statements setup in this manner: case "10:00", "11:20", "13:30": Do something break; case "17:00", "19:20", "23:30": Do something else break; default break; Can anyone pinpoint my syntax error with the cases I have defined above. When I used VB, this syntax worked fine with the multiple points but it doesn't now. Any pointers would be greatly appreciated.

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

      switch(time)
      {
      case "10:00":
      case "11:20":
      case "13:30":
      // Do Something
      break;

      //etc
      }

      No multiple params in a switch case, fallthrough only allowed when no code is defined in that particular case.

      F 1 Reply Last reply
      0
      • J J4amieC

        switch(time)
        {
        case "10:00":
        case "11:20":
        case "13:30":
        // Do Something
        break;

        //etc
        }

        No multiple params in a switch case, fallthrough only allowed when no code is defined in that particular case.

        F Offline
        F Offline
        ffowler
        wrote on last edited by
        #3

        Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.

        L F P 3 Replies Last reply
        0
        • F ffowler

          Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          ffowler wrote:

          From your snippet, at 10:00 // Do Something will be performed?

          Yes, no need to worry about failing memory yet :)

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          1 Reply Last reply
          0
          • F ffowler

            Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.

            F Offline
            F Offline
            ffowler
            wrote on last edited by
            #5

            Wheeww. That's a relief. Thank you very much for your prompt responses (and diagnosis).

            1 Reply Last reply
            0
            • F ffowler

              Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Yes - because switch statements like this fall through to the next case. Note that they only work if there's nothing betweent the case statements. As soon as you introduce an operation, you have to use break;.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              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