switch case

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Roberts

    #1

    switch case

    In VB6 I could do the following

    Select Case Right$(Text, 1)
    Case "a","b"
    do this
    Case "c","d"
    do this
    Case else
    do this
    End Select


    can I have multiple items per case in c#?

    Thanks
    Alan


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: switch case

    Alan,

    Yes, but you would have to do it differently:

    switch (Text.Substring (Text.Length - 1, 1))
    {
    case "a":
    case "b":
    // Do something

    break;

    case "c":
    case "d":
    // Do something else.

    break;

    default:

    // Default case.
    }

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Alan Roberts" <alan@statistix l.co.uk> wrote in message
    news:uXq20dMBGH A.3164@TK2MSFTN GP10.phx.gbl...[color=blue]
    > In VB6 I could do the following
    >
    > Select Case Right$(Text, 1)
    > Case "a","b"
    > do this
    > Case "c","d"
    > do this
    > Case else
    > do this
    > End Select
    >
    >
    > can I have multiple items per case in c#?
    >
    > Thanks
    > Alan
    >[/color]


    Comment

    • Alan Roberts

      #3
      Re: switch case

      Thanks Nicholas. A bit more cumbersome but should do the trick!

      Thanks again

      Alan

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:u4$SlhMBGH A.2708@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Alan,
      >
      > Yes, but you would have to do it differently:
      >
      > switch (Text.Substring (Text.Length - 1, 1))
      > {
      > case "a":
      > case "b":
      > // Do something
      >
      > break;
      >
      > case "c":
      > case "d":
      > // Do something else.
      >
      > break;
      >
      > default:
      >
      > // Default case.
      > }
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Alan Roberts" <alan@statistix l.co.uk> wrote in message
      > news:uXq20dMBGH A.3164@TK2MSFTN GP10.phx.gbl...[color=green]
      >> In VB6 I could do the following
      >>
      >> Select Case Right$(Text, 1)
      >> Case "a","b"
      >> do this
      >> Case "c","d"
      >> do this
      >> Case else
      >> do this
      >> End Select
      >>
      >>
      >> can I have multiple items per case in c#?
      >>
      >> Thanks
      >> Alan
      >>[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: switch case

        Alan Roberts <alan@statistix l.co.uk> wrote:[color=blue]
        > Thanks Nicholas. A bit more cumbersome but should do the trick![/color]

        Slightly less cumbersome would be to use the indexer instead of
        Substring - this only works for a single character, however. (Note the
        single quotes instead of double quotes):

        switch (Text[Text.Length-1])
        {
        case 'a':
        case 'b':
        // Do something
        break;
        case 'c':
        case 'd':
        // Do something else.
        break;
        default:
        // Default case.
        }

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...