switch statement in c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • genc ymeri

    switch statement in c#

    Hi,
    I'm trying to do a very easy thing in SWITCH statment in C# but for some
    reasons I can't do it (I'm getting errors messages,probab ly I'm missing
    something)

    What I want to do is this :

    int n = aObject.Functio nOfIntergerType ;

    switch (n)
    {
    case 1,2 : dothis; // <- how can I group multiple values in one case
    ?????
    case 3 :dotThat;
    }

    Thank You.


  • Morten Wennevik

    #2
    Re: switch statement in c#

    Hi,

    You can't have multiple case statements like that in C#.
    However, in simple cases like that you can allow a fallthrough to the next statement.

    switch(n)
    {
    case 1:
    case 2:
    dothis;
    break;
    case 3:
    dothat;
    break;
    }

    A fallthrough is only allowed if the case is empty.

    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • Chua Wen Ching

      #3
      RE: switch statement in c#

      Hi Genc,

      You can do a fall through like what Morten recommended or you can use goto
      statement in you swicth statements :)

      case 1:
      goto case 3;
      case 2:
      Console.WriteLi ne("OOps");
      break;
      case 3:
      Console.WriteLi ne("Nah");
      break;
      default:
      Console.WriteLi ne("Wrong");
      break;

      Have fun!

      "genc ymeri" wrote:
      [color=blue]
      > Hi,
      > I'm trying to do a very easy thing in SWITCH statment in C# but for some
      > reasons I can't do it (I'm getting errors messages,probab ly I'm missing
      > something)
      >
      > What I want to do is this :
      >
      > int n = aObject.Functio nOfIntergerType ;
      >
      > switch (n)
      > {
      > case 1,2 : dothis; // <- how can I group multiple values in one case
      > ?????
      > case 3 :dotThat;
      > }
      >
      > Thank You.
      >
      >
      >[/color]

      Comment

      • genc ymeri

        #4
        Re: switch statement in c#

        thanks a lot :)

        "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
        news:opsexnjavj klbvpo@stone...[color=blue]
        > Hi,
        >
        > You can't have multiple case statements like that in C#.
        > However, in simple cases like that you can allow a fallthrough to the next[/color]
        statement.[color=blue]
        >
        > switch(n)
        > {
        > case 1:
        > case 2:
        > dothis;
        > break;
        > case 3:
        > dothat;
        > break;
        > }
        >
        > A fallthrough is only allowed if the case is empty.
        >
        > --
        > Happy coding!
        > Morten Wennevik [C# MVP][/color]


        Comment

        • genc ymeri

          #5
          Re: switch statement in c#

          thnx :)

          "Chua Wen Ching" <chua_wen_ching @nospam.hotmail .com> wrote in message
          news:4CB9F000-BA9D-49EE-8BD6-CB358F2AE454@mi crosoft.com...[color=blue]
          > Hi Genc,
          >
          > You can do a fall through like what Morten recommended or you can use goto
          > statement in you swicth statements :)
          >
          > case 1:
          > goto case 3;
          > case 2:
          > Console.WriteLi ne("OOps");
          > break;
          > case 3:
          > Console.WriteLi ne("Nah");
          > break;
          > default:
          > Console.WriteLi ne("Wrong");
          > break;
          >
          > Have fun!
          >
          > "genc ymeri" wrote:
          >[color=green]
          > > Hi,
          > > I'm trying to do a very easy thing in SWITCH statment in C# but for some
          > > reasons I can't do it (I'm getting errors messages,probab ly I'm missing
          > > something)
          > >
          > > What I want to do is this :
          > >
          > > int n = aObject.Functio nOfIntergerType ;
          > >
          > > switch (n)
          > > {
          > > case 1,2 : dothis; // <- how can I group multiple values in one case
          > > ?????
          > > case 3 :dotThat;
          > > }
          > >
          > > Thank You.
          > >
          > >
          > >[/color][/color]


          Comment

          Working...