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. using a switch for a range of numbers

using a switch for a range of numbers

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

    Sorry for the basic post, but I have looked everywhere and I can't figure out if this is possible or not. I am wondering if it is possible to check and variable to see if it is between certain ranges of numbers using a switch statement. Ex: switch (int){ case (between 1 and 10): case (between 11 and 20: and so on. Let me know if know. Jason

    L L 3 Replies Last reply
    0
    • F fou99004

      Sorry for the basic post, but I have looked everywhere and I can't figure out if this is possible or not. I am wondering if it is possible to check and variable to see if it is between certain ranges of numbers using a switch statement. Ex: switch (int){ case (between 1 and 10): case (between 11 and 20: and so on. Let me know if know. Jason

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

      you can preprocess the number a bit beforehand like switch(int/10) leppie::AllocCPArticle("Zee blog");
      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

      1 Reply Last reply
      0
      • F fou99004

        Sorry for the basic post, but I have looked everywhere and I can't figure out if this is possible or not. I am wondering if it is possible to check and variable to see if it is between certain ranges of numbers using a switch statement. Ex: switch (int){ case (between 1 and 10): case (between 11 and 20: and so on. Let me know if know. Jason

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

        Another way is to combine (encode) 2 numbers into 1 eg: Required range: 22-63 int range = (22 << 16) + (63 & 0xFFFF); leppie::AllocCPArticle("Zee blog");
        Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

        1 Reply Last reply
        0
        • F fou99004

          Sorry for the basic post, but I have looked everywhere and I can't figure out if this is possible or not. I am wondering if it is possible to check and variable to see if it is between certain ranges of numbers using a switch statement. Ex: switch (int){ case (between 1 and 10): case (between 11 and 20: and so on. Let me know if know. Jason

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

          This is one of those features that VB has that I wish C# supported. You can just add case statements for each of the values adding the break statement to the last case in the range switch (value) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: DoSomething (); break; } not pretty but it works.

          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