I suggest you ...

Modify switch case to work like VB Select

Modify the Switch Case so it has the same capabilities as the VB Select Case, Case statements don't have to be constants

switch ( true){
case (x==1 && y==3):
break;
case (x==1&&y>3):
break;
case x == 2:
break;
}

53 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Matthew C. Hintzen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.

- The Visual Studio Team

7 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Marc commented  ·   ·  Flag as inappropriate

    I agree, the syntax is pedantic trash. Take for example the VB code

    Select Case i
    Case 1, 3, 6, 7, 9 : s = "A"
    Case 2, 4, 8, 10, 44 To 52, 56 : s = "B"
    Case 53, 54, 55, 57, 58 : s = "C"
    Case Else : Return ""
    End Select

    in C# this is:

    switch (i)
    {
    case 1:
    case 3:
    case 6:
    case 7:
    case 9:
    s = "A";
    break;
    case 2:
    case 4:
    case 8:
    case 10:
    case 44:
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 56:
    s = "B";
    break;
    case 53:
    case 54:
    case 55:
    case 57:
    case 58:
    s = "C";
    break;
    default:
    return "";
    }

    Or you could go for:

    if (((((i == 1) || (i == 3)) || (i == 6)) || (i == 7)) || (i == 9))
    {
    s = "A";
    }
    else if ((((((i == 2) || (i == 4)) || (i == 8)) || (i == 10)) || (i >= 44 && i <= 52)) || (i == 56))
    {
    s = "B";
    }
    else if (((((i == 53) || (i == 54)) || (i == 55)) || (i == 57)) || (i == 58))
    {
    s = "C";
    }
    else
    {
    return "";
    }

  • Hinze commented  ·   ·  Flag as inappropriate

    I used to be a VB programmer, now I am forced to write C#.
    This is the thing that drives me crazy most when writing C#.
    The Select Case of VB is really awesome. It executes not more than one Case branch.
    Examples:

    With integer:

    Select Case n
    Case 1, 2, 3, 4 'list of several values
    Case 1 to 7 'range of values
    Case Is > 10 'inequality
    Case 1, 2, 7 to 10, Is > 12 'all combined (imagine this as a boolean expression!)
    Case Else
    End Select

    With string:

    Select Case text
    Case "abc", "fgh"
    Case "A" To "Z"
    End Select

    With object (in the next version of VB):

    Select Case sender
    Case Is btnOK
    Case Is btnCancel
    End Select

    And all this without stupid "break;".

  • Francis Rodgers commented  ·   ·  Flag as inappropriate

    Either switch case is good or it is not. (I think it is good). But for those that argue against it, why not remove it all together and just use if statements. Many who argue that switch cases should not be extended as in this post. Argue also that it should not be removed. Most would agree that in some cases the code is indeed cleaner in a switch case. We are programmers, so let us decide what to use, and if you will provide a switch case, provide a powerful one or remove it all together. When I use C# and it is my main language, I hate programming long winded If else if clauses when I know a simple switch case would be far better. I miss that functionality in VB and often as a result, I revert either to polymorphism to replace the switch case all togeher, or I try to develop that part of the program in a VB dll class and use it in C#/ Both of which add much more complexity to my code.

  • Tobias Ickler commented  ·   ·  Flag as inappropriate

    well, there is a difference between switch and if / else if / else if / else... I guess this difference should be maintained

Feedback and Knowledge Base