CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    [RESOLVED] .NET4.0/VS 2010>>Need "Case 0 to 8" without using Fall Thru

    Hi!

    In VB I can use the Select Case function and specify case 0 to 8, or Case a to b

    How can I do this in C# without using fall thru?

    Thanks!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: .NET4.0/VS 2010>>Need "Case 0 to 8" without using Fall Thru

    Well you can't; without fallthrough it obviously won't work. Your goals are contradictory. You can of course use fallthrough in C# as long as there is no code in any but the last case:

    Code:
    switch( someVar )
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
          DoStuff();
          break;
    }
    However, if it is that trivial then you can just use an if statement:

    Code:
    if( someVar > 0 && someVar < 9 ) { }
    
    OR, the more expensive and a bit superfluous version...
    
    if( Enumerable.Range( 0, 8 ).Contains( someVar ) ) { }
    Last edited by BigEd781; September 9th, 2011 at 02:46 PM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: .NET4.0/VS 2010>>Need "Case 0 to 8" without using Fall Thru

    You can't do it with the switch statement.

    You'll need to use another type of construct like an if or if/else statement.

  4. #4
    Join Date
    Feb 2003
    Location
    Ft. Worth Texas
    Posts
    31

    Re: .NET4.0/VS 2010>>Need "Case 0 to 8" without using Fall Thru

    Yeah, I was about half afraid that was the case. I'll use the if statement.

    Thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured