switch case with true

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric S.

    switch case with true

    Why doesn't something like this work? I get "a constant value is
    expected" on all 3 case statements.

    private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
    {
    try
    {
    switch (true)
    {
    case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
    throw new Exception();
    break;
    case firstDg.Items.C ount != 0:
    return firstDg;
    case secondDg.Items. Count != 0:
    return secondDg;
    default:
    return null;
    }
    }
    catch
    {
    return null;
    }
    }

  • Carlos J. Quintero [.NET MVP]

    #2
    Re: switch case with true

    The values in C# case statements must be constants (at compile-time) and you
    are using variables such as firstDg.Items.C ount. Case statements of VB.NET
    (or VB6) do not have such restriction, just in case you come from that
    background.

    --

    Carlos J. Quintero

    MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
    You can code, design and document much faster.
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



    "Eric S." <mopar41@hotmai l.com> escribió en el mensaje
    news:1105712621 .517839.303330@ c13g2000cwb.goo glegroups.com.. .[color=blue]
    > Why doesn't something like this work? I get "a constant value is
    > expected" on all 3 case statements.
    >
    > private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
    > {
    > try
    > {
    > switch (true)
    > {
    > case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
    > throw new Exception();
    > break;
    > case firstDg.Items.C ount != 0:
    > return firstDg;
    > case secondDg.Items. Count != 0:
    > return secondDg;
    > default:
    > return null;
    > }
    > }
    > catch
    > {
    > return null;
    > }
    > }
    >[/color]


    Comment

    • Lasse Vågsæther Karlsen

      #3
      Re: switch case with true

      Eric S. wrote:[color=blue]
      > Why doesn't something like this work? I get "a constant value is
      > expected" on all 3 case statements.
      >[/color]
      <snip>

      Let's say the example was compilable. If two of those compiled to true,
      what then ? I know that that's not a valid case in your example, but the
      compiler needs to guarantee that it only takes one branch, and adding
      static expression analysis to this case would make it really hard for
      the compiler to guarantee that.

      The syntax for switch requires you to use constants for the cases, so
      that's what you have to do. In your case I would go with a couple of
      if-statements.

      --
      Lasse Vågsæther Karlsen

      mailto:lasse@vk arlsen.no
      PGP KeyID: 0x0270466B

      Comment

      • Ignacio Machin \( .NET/ C#  MVP \)

        #4
        Re: switch case with true

        Hi,

        and you may getting also a warning, saying something about a constant value
        being used in a switch

        the case(s) labels should be constants, it cannot be an expression that need
        to be evaluated in runtime, its values need to be know at compile time as
        the compiler generate the jumps instructions.

        In your case you can use several if cnostructions.


        Cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation





        "Eric S." <mopar41@hotmai l.com> wrote in message
        news:1105712621 .517839.303330@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > Why doesn't something like this work? I get "a constant value is
        > expected" on all 3 case statements.
        >
        > private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
        > {
        > try
        > {
        > switch (true)
        > {
        > case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
        > throw new Exception();
        > break;
        > case firstDg.Items.C ount != 0:
        > return firstDg;
        > case secondDg.Items. Count != 0:
        > return secondDg;
        > default:
        > return null;
        > }
        > }
        > catch
        > {
        > return null;
        > }
        > }
        >[/color]


        Comment

        • Ignacio Machin \( .NET/ C#  MVP \)

          #5
          Re: switch case with true A LITTLE OFFTOPICS Comments required

          Hi

          I would like some comments about the use a while/break in a multiple case
          escenario as described in the parent post.

          Now thinking about what I said before regarding switch and constants
          expression, it should be common the situation where you have multiple
          exclusives cases ( as in a switch) but the decision of which one to execute
          is not constant ( so you cannot use switch ) thinking in this I saw a couple
          of solutions

          1- multiple if/elseif it may get cumbersome to understand the code,
          especially if the editor does not format it correctly.

          2-Use an auxiliary method , where each possible case ends with a return ,
          this is a better idea but it can get complex if any case use local variables
          , in this escenario it will be necessary to pass several parameters and it
          may get difficult to understand.

          3- And this is the escenario I would like to hear opinion about , using a
          while ( true ) as a way to avoid using the "else if" constructions and
          without moving the code to another method
          if would looks like

          while( true )
          {
          if ( case 1 )
          {
          .....
          break;
          }

          if ( case 2 )
          {
          .....
          break;
          }

          if ( case 3 )
          {
          .....
          break;
          }



          break;
          }



          The code looks clean, there are not a chained if string and do what is
          expected to execute at least one case

          Has anybody use this construction before?

          What are your comments about it?

          Cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          "Eric S." <mopar41@hotmai l.com> wrote in message
          news:1105712621 .517839.303330@ c13g2000cwb.goo glegroups.com.. .[color=blue]
          > Why doesn't something like this work? I get "a constant value is
          > expected" on all 3 case statements.
          >
          > private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
          > {
          > try
          > {
          > switch (true)
          > {
          > case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
          > throw new Exception();
          > break;
          > case firstDg.Items.C ount != 0:
          > return firstDg;
          > case secondDg.Items. Count != 0:
          > return secondDg;
          > default:
          > return null;
          > }
          > }
          > catch
          > {
          > return null;
          > }
          > }
          >[/color]


          Comment

          • carl.manaster@gmail.com

            #6
            Re: switch case with true A LITTLE OFFTOPICS Comments required

            Hi, Ignacio,

            I don't like it, personally, because I don't find it very
            intention-revealing. "while," to me, carries an implication of
            repetition. I like your option 2 much better.

            Also, for mutually exclusive cases, "else if" is only a matter of
            efficiency, not necessity - you can simply put the "if" statements in
            sequence. Sometimes I find this cleaner. I don't much like "else if":

            <http://onlysyntax.blog spot.com/2003/04/conditionals-else-if-to-me-one-clue.html>
            Peace,
            --Carl

            Comment

            • carl.manaster@gmail.com

              #7
              Re: switch case with true A LITTLE OFFTOPICS Comments required

              Hi, Ignacio,

              I don't like it, personally, because I don't find it very
              intention-revealing. "while," to me, carries an implication of
              repetition. I like your option 2 much better.

              Also, for mutually exclusive cases, "else if" is only a matter of
              efficiency, not necessity - you can simply put the "if" statements in
              sequence. Sometimes I find this cleaner. I don't much like "else if":

              <http://onlysyntax.blog spot.com/2003/04/conditionals-else-if-to-me-one-clue.html>
              Peace,
              --Carl

              Comment

              • Nick Malik [Microsoft]

                #8
                Re: switch case with true A LITTLE OFFTOPICS Comments required

                I've not used "while(true )" because a mistake in coding could lead to an
                infinite loop.
                On the other hand, I have used your "method 2" where I created seperate
                private methods that effectively did the same thing, only they used the
                "return" to leave the method and return to the caller. You are right that
                this isn't always applicable if you have a very large number of local
                variables.

                Another common approach is to set a "done flag"

                bool done = false;
                if (condition1 and not done)
                {
                // do stuff
                done = true;
                }
                if (condition2 and ! done)
                {
                // do stuff
                done = true;
                }


                However, none of these are particularly object oriented. The OO answer to
                this is the Chain of Responsibility pattern.
                Chain of Responsibility - Example C# Design Pattern in C#. 100% Source code.


                Hope this helps,

                --
                --- Nick Malik [Microsoft]
                MCSD, CFPS, Certified Scrummaster


                Disclaimer: Opinions expressed in this forum are my own, and not
                representative of my employer.
                I do not answer questions on behalf of my employer. I'm just a
                programmer helping programmers.
                --
                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
                in message news:OQtRomk%23 EHA.2112@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                > Hi
                >
                > I would like some comments about the use a while/break in a multiple case
                > escenario as described in the parent post.
                >
                > Now thinking about what I said before regarding switch and constants
                > expression, it should be common the situation where you have multiple
                > exclusives cases ( as in a switch) but the decision of which one to[/color]
                execute[color=blue]
                > is not constant ( so you cannot use switch ) thinking in this I saw a[/color]
                couple[color=blue]
                > of solutions
                >
                > 1- multiple if/elseif it may get cumbersome to understand the code,
                > especially if the editor does not format it correctly.
                >
                > 2-Use an auxiliary method , where each possible case ends with a return ,
                > this is a better idea but it can get complex if any case use local[/color]
                variables[color=blue]
                > , in this escenario it will be necessary to pass several parameters and it
                > may get difficult to understand.
                >
                > 3- And this is the escenario I would like to hear opinion about , using a
                > while ( true ) as a way to avoid using the "else if" constructions and
                > without moving the code to another method
                > if would looks like
                >
                > while( true )
                > {
                > if ( case 1 )
                > {
                > .....
                > break;
                > }
                >
                > if ( case 2 )
                > {
                > .....
                > break;
                > }
                >
                > if ( case 3 )
                > {
                > .....
                > break;
                > }
                >
                >
                >
                > break;
                > }
                >
                >
                >
                > The code looks clean, there are not a chained if string and do what is
                > expected to execute at least one case
                >
                > Has anybody use this construction before?
                >
                > What are your comments about it?
                >
                > Cheers,
                >
                > --
                > Ignacio Machin,
                > ignacio.machin AT dot.state.fl.us
                > Florida Department Of Transportation
                >
                >
                > "Eric S." <mopar41@hotmai l.com> wrote in message
                > news:1105712621 .517839.303330@ c13g2000cwb.goo glegroups.com.. .[color=green]
                > > Why doesn't something like this work? I get "a constant value is
                > > expected" on all 3 case statements.
                > >
                > > private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
                > > {
                > > try
                > > {
                > > switch (true)
                > > {
                > > case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
                > > throw new Exception();
                > > break;
                > > case firstDg.Items.C ount != 0:
                > > return firstDg;
                > > case secondDg.Items. Count != 0:
                > > return secondDg;
                > > default:
                > > return null;
                > > }
                > > }
                > > catch
                > > {
                > > return null;
                > > }
                > > }
                > >[/color]
                >
                >[/color]


                Comment

                • Ignacio Machin \( .NET/ C#  MVP \)

                  #9
                  Re: switch case with true A LITTLE OFFTOPICS Comments required

                  Hi,

                  [color=blue]
                  > I don't like it, personally, because I don't find it very
                  > intention-revealing. "while," to me, carries an implication of
                  > repetition. I like your option 2 much better.[/color]

                  I think the same, it may be confusing but I found it interesting anyway. I
                  had never thought about that until I was writing the first answer.
                  Option 2 is workable when there are not lots of locals variables involved,
                  it should be easier with VS.net 05 and refactoring though.



                  Cheers,

                  --
                  Ignacio Machin,
                  ignacio.machin AT dot.state.fl.us
                  Florida Department Of Transportation


                  Comment

                  • Ignacio Machin \( .NET/ C#  MVP \)

                    #10
                    Re: switch case with true A LITTLE OFFTOPICS Comments required

                    Hi,

                    Wont it better if the done flag is the first conditional?
                    if you put it in the last it will evaluate the others anyway and this may
                    not be very efficient.

                    Cheers,

                    --
                    Ignacio Machin,
                    ignacio.machin AT dot.state.fl.us
                    Florida Department Of Transportation


                    "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.com> wrote in message
                    news:ce6dnU_yLc 5Ic3rcRVn-qw@comcast.com. ..[color=blue]
                    > I've not used "while(true )" because a mistake in coding could lead to an
                    > infinite loop.
                    > On the other hand, I have used your "method 2" where I created seperate
                    > private methods that effectively did the same thing, only they used the
                    > "return" to leave the method and return to the caller. You are right that
                    > this isn't always applicable if you have a very large number of local
                    > variables.
                    >
                    > Another common approach is to set a "done flag"
                    >
                    > bool done = false;
                    > if (condition1 and not done)
                    > {
                    > // do stuff
                    > done = true;
                    > }
                    > if (condition2 and ! done)
                    > {
                    > // do stuff
                    > done = true;
                    > }
                    >
                    >
                    > However, none of these are particularly object oriented. The OO answer to
                    > this is the Chain of Responsibility pattern.
                    > http://www.dofactory.com/Patterns/PatternChain.aspx
                    >
                    > Hope this helps,
                    >
                    > --
                    > --- Nick Malik [Microsoft]
                    > MCSD, CFPS, Certified Scrummaster
                    > http://blogs.msdn.com/nickmalik
                    >
                    > Disclaimer: Opinions expressed in this forum are my own, and not
                    > representative of my employer.
                    > I do not answer questions on behalf of my employer. I'm just a
                    > programmer helping programmers.
                    > --
                    > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
                    > wrote
                    > in message news:OQtRomk%23 EHA.2112@TK2MSF TNGP14.phx.gbl. ..[color=green]
                    >> Hi
                    >>
                    >> I would like some comments about the use a while/break in a multiple
                    >> case
                    >> escenario as described in the parent post.
                    >>
                    >> Now thinking about what I said before regarding switch and constants
                    >> expression, it should be common the situation where you have multiple
                    >> exclusives cases ( as in a switch) but the decision of which one to[/color]
                    > execute[color=green]
                    >> is not constant ( so you cannot use switch ) thinking in this I saw a[/color]
                    > couple[color=green]
                    >> of solutions
                    >>
                    >> 1- multiple if/elseif it may get cumbersome to understand the code,
                    >> especially if the editor does not format it correctly.
                    >>
                    >> 2-Use an auxiliary method , where each possible case ends with a return ,
                    >> this is a better idea but it can get complex if any case use local[/color]
                    > variables[color=green]
                    >> , in this escenario it will be necessary to pass several parameters and
                    >> it
                    >> may get difficult to understand.
                    >>
                    >> 3- And this is the escenario I would like to hear opinion about , using
                    >> a
                    >> while ( true ) as a way to avoid using the "else if" constructions and
                    >> without moving the code to another method
                    >> if would looks like
                    >>
                    >> while( true )
                    >> {
                    >> if ( case 1 )
                    >> {
                    >> .....
                    >> break;
                    >> }
                    >>
                    >> if ( case 2 )
                    >> {
                    >> .....
                    >> break;
                    >> }
                    >>
                    >> if ( case 3 )
                    >> {
                    >> .....
                    >> break;
                    >> }
                    >>
                    >>
                    >>
                    >> break;
                    >> }
                    >>
                    >>
                    >>
                    >> The code looks clean, there are not a chained if string and do what is
                    >> expected to execute at least one case
                    >>
                    >> Has anybody use this construction before?
                    >>
                    >> What are your comments about it?
                    >>
                    >> Cheers,
                    >>
                    >> --
                    >> Ignacio Machin,
                    >> ignacio.machin AT dot.state.fl.us
                    >> Florida Department Of Transportation
                    >>
                    >>
                    >> "Eric S." <mopar41@hotmai l.com> wrote in message
                    >> news:1105712621 .517839.303330@ c13g2000cwb.goo glegroups.com.. .[color=darkred]
                    >> > Why doesn't something like this work? I get "a constant value is
                    >> > expected" on all 3 case statements.
                    >> >
                    >> > private DataGrid CurrentDataGrid (DataGrid firstDg, DataGrid secondDg)
                    >> > {
                    >> > try
                    >> > {
                    >> > switch (true)
                    >> > {
                    >> > case firstDg.Items.C ount != 0 & secondDg.Items. Count != 0:
                    >> > throw new Exception();
                    >> > break;
                    >> > case firstDg.Items.C ount != 0:
                    >> > return firstDg;
                    >> > case secondDg.Items. Count != 0:
                    >> > return secondDg;
                    >> > default:
                    >> > return null;
                    >> > }
                    >> > }
                    >> > catch
                    >> > {
                    >> > return null;
                    >> > }
                    >> > }
                    >> >[/color]
                    >>
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    Working...