Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
how to use switch case for a range of number

Answered how to use switch case for a range of number

  • Saturday, August 01, 2009 3:37 PM
     
      Has Code
    As above...  could this statement work ?  case (0 - 500)
    public void Switch(int num)
    {
      switch (num)
      {
        case (0-500):
          // belong to 0-500;
         break;
        case (501-1000):
           // belong to 501-1000;
          break;
      }
    }

All Replies

  • Saturday, August 01, 2009 3:48 PM
     
     
    Have you tried it?  Posting takes longer than testing in the IDE.
  • Saturday, August 01, 2009 3:50 PM
     
     Answered
    You can't really do that with a switch.

    You can do:

    if (num >= 0 && num <= 500) { ........}
    else if (num >= 501 && num <= 1000) {.............}
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Saturday, August 01, 2009 4:00 PM
     
     Answered Has Code
    You can do it with small ranges of numbers like so:

    switch (myInt)
    {
        case 0:
        case 1:
        case 2:
            // do something
            break;
        etc...
    }
    But I definately wouldn't recommend that for 500 of em.
  • Saturday, August 01, 2009 4:00 PM
     
     
    JohnWein, that doesn't work in C# unfortunately. The switch only deals with constants

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Saturday, August 01, 2009 4:01 PM
     
     Answered
    The answer is NO!

    See the same question and answer here:
    http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c46bee82-ac9a-4f15-bcc0-9e5baa557916/

    VB can do it but not C#. People suggested using if/else.

    Oh, by the way, (0 - 500) is an expression and
    (0 - 500) = -500 




    John Chen -- See my team blog: http://blogs.msdn.com/vsdata
  • Saturday, August 01, 2009 4:06 PM
     
     
    JohnWein, that doesn't work in C# unfortunately. The switch only deals with constants

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    What doesn't work in C#?  You can certainly write code that doesn't work in the IDE.  Try it.
  • Saturday, August 01, 2009 4:13 PM
     
      Has Code
    Read the documentation here on switch

    switch (expression)
    {
    case constant-expression :
    statement
    jump-statement
    [default:
    statement
    jump-statement]
    }

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Saturday, August 01, 2009 4:36 PM
     
      Has Code
    Read the documentation here on switch

    switch (expression)
    {
    case constant-expression :
    statement
    jump-statement
    [default:
    statement
    jump-statement]
    }

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    If this post is for my benefit, read my original post in this thread.  The OP should have been able to answer his question without posting it to a forum.  If you need assistance start a new thread.
  • Saturday, August 01, 2009 4:59 PM
     
     Answered

    Hi,
    After I see all this answers there is no comment they are right you can't, so the best solution With such large ranges it is easier to use if - else if statements.

     Thanks

     


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    My Blog
  • Saturday, August 01, 2009 5:08 PM
     
     
    It wasn't directed at you JohnWein, it was meant for all, generally speaking.

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Saturday, August 01, 2009 5:24 PM
    Moderator
     
     Answered
    As long as it is a range with a constant interval, you can map the range to an integer:

      int range = (num-1) / 500;
      switch (range) {
        case 0: break; // 1-500
        case 1: break; // 501-1000
        // etc...
      }

    if/else for non-constant intervals.



    Hans Passant.