C# Switch statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Duncan  McNutt

    #16
    Re: C# Switch statement

    Absolutely, without MS products our team wouldnt exist :D

    --

    Duncan McNutt
    Microsoft Product Deactivation Team
    --


    "Edward Yang" <neo_in_matrix@ msn.com> wrote in message
    news:##hH3RYZDH A.1280@tk2msftn gp13.phx.gbl...[color=blue]
    > Yes, there is no absolute EQUALITY in this world. So C# != C/C++. I quite
    > agree.
    >
    > But C# borrows or inherits much of C/C++'s language infrastructures . So[/color]
    what[color=blue]
    > I said in my previous post is applicable to C#.
    >
    > BTW, what is Microsoft Product Deactivation Team? Is it funded by M$? :-p
    >
    > Edward
    >
    > "Duncan McNutt" <mutmaster@127. 0.0.22> wrote in message
    > news:em6YfCXZDH A.1872@TK2MSFTN GP12.phx.gbl...[color=green]
    > > C# isnt C/C++, thats the point MC++ is just a strap on for .NET C# was
    > > designed from the ground up for .NET yet they left things as it was, and[/color]
    > now[color=green]
    > > we're gettin partial types and lambada funtions (anonymous methods)[/color][/color]
    expect[color=blue][color=green]
    > > spaghetii code now, generic types is the only real biggie comming in 04[/color]
    > that[color=green]
    > > I can see, the rest is just gonna cause more problems later on I think.
    > >
    > > --
    > >
    > > Duncan McNutt
    > > Microsoft Product Deactivation Team
    > > --
    > >
    > >
    > > "Edward Yang" <neo_in_matrix@ msn.com> wrote in message
    > > news:eSg5z#WZDH A.1384@TK2MSFTN GP10.phx.gbl...[color=darkred]
    > > > 1..9 or 1 to 9 seems to be 'clean' code.
    > > >
    > > > But, the real truth is that I think the stack of case's is cleaner[/color][/color][/color]
    code.[color=blue][color=green][color=darkred]
    > > >
    > > > And, as a C/C++ programmer, we guard cautiously our religion[/color][/color][/color]
    principles[color=blue][color=green]
    > > :-).[color=darkred]
    > > > We do not want to borrow something from another religion.
    > > >
    > > > Edward
    > > >
    > > > "Duncan McNutt" <mutmaster@127. 0.0.22> wrote in message
    > > > news:%238uahxWZ DHA.1280@tk2msf tngp13.phx.gbl. ..
    > > > > But isnt it more code clean and easier to have
    > > > >
    > > > > case 1..9:
    > > > > or
    > > > > case 1 to 9:
    > > > >
    > > > > instead of
    > > > >
    > > > > case 1:
    > > > > case 2:
    > > > > case 3:
    > > > > case 4:
    > > > > case 5:
    > > > > case 6:
    > > > > case 7:
    > > > > case 8:
    > > > > case 9:
    > > > > {
    > > > > blah();
    > > > > break;
    > > > > }
    > > > >
    > > > > get my drift?
    > > > >
    > > > > I thought C# and .net was about making it more PROBLEM orientated[/color][/color][/color]
    and[color=blue][color=green][color=darkred]
    > > > > elegant solutions, and not house cleaning of code, well this is one[/color][/color]
    > case[color=green][color=darkred]
    > > > > that they have failed in my view.
    > > > >
    > > > >
    > > > >
    > > > > --
    > > > >
    > > > > Duncan McNutt
    > > > > Microsoft Product Deactivation Team
    > > > > --
    > > > >
    > > > >
    > > > > "Edward Yang" <neo_in_matrix@ msn.com> wrote in message
    > > > > news:ulyaMoSZDH A.1872@TK2MSFTN GP12.phx.gbl...
    > > > > > OMG, I thought it is not allowed to do this!
    > > > > >
    > > > > > I read MSDN again, and find this:
    > > > > >
    > > > > > Although fall through from one case label to another is not[/color][/color]
    > supported,[color=green][color=darkred]
    > > > it
    > > > > is
    > > > > > allowed to stack case labels, for example:
    > > > > > case 0:
    > > > > > case 1:
    > > > > > // do something;
    > > > > > Edward
    > > > > >
    > > > > > "Stan" <stanly12spamre move@yahoo.com. au> wrote in message
    > > > > > news:eFxvT3RZDH A.652@tk2msftng p13.phx.gbl...
    > > > > > > Here, I did a search and copied one of the many replies to this[/color]
    > > common[color=darkred]
    > > > > > > question.
    > > > > > > >
    > > > > > >
    > > > > > > Yes, like this:
    > > > > > >
    > > > > > > using System;
    > > > > > >
    > > > > > > class Test
    > > > > > > {
    > > > > > >
    > > > > > > public static void Main()
    > > > > > > {
    > > > > > > int x;
    > > > > > > x=Convert.ToInt 16(Console.Read Line());
    > > > > > > switch (x)
    > > > > > > {
    > > > > > > case 1:
    > > > > > > case 2:
    > > > > > > case 3:
    > > > > > > Console.WriteLi ne("Is 1 2 3");
    > > > > > > break;
    > > > > > > case 4:
    > > > > > > case 5:
    > > > > > > case 6:
    > > > > > > Console.WriteLi ne("Is 4 5 6");
    > > > > > > break;
    > > > > > > default:
    > > > > > > Console.WriteLi ne("Is greater than 6");
    > > > > > > break;
    > > > > > > }
    > > > > > > }
    > > > > > > }
    > > > > > >
    > > > > > > However, you can't do greater than/less than (i.e. case 4 to 6)[/color][/color]
    > like[color=green][color=darkred]
    > > > > > > you could do in VB. You'll have to use if/else statements for[/color][/color]
    > that.[color=green][color=darkred]
    > > > > > >
    > > > > > > 'Lib
    > > > > > >
    > > > > > >
    > > > > > >
    > > > > > > "Duncan McNutt [BSDM]" <nutmaster@127. 0.0.66> wrote in message
    > > > > > > news:eNPUQtPZDH A.2352@TK2MSFTN GP12.phx.gbl...
    > > > > > > > Is it possible to have ranges or patterns in a case ?
    > > > > > > >
    > > > > > > > If not, why wasnt this designed in to make it easier instead[/color][/color][/color]
    of[color=blue][color=green][color=darkred]
    > > > > listing
    > > > > > > > every case value needed?
    > > > > > > >
    > > > > > > > --
    > > > > > > >
    > > > > > > > Duncan McNutt
    > > > > > > > Microsoft Product Deactivation Team
    > > > > > > > --
    > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >[/color]
    > >
    > >[/color]
    >
    >[/color]


    Comment

    • Peter Vidler

      #17
      Re: C# Switch statement

      Hi,
      [color=blue]
      > but I do NOT remember I ever made a mistake with case-break's.[/color]

      Maybe YOU don't (doubtful), but many people do. Hence the design decision.

      You almost never want a case statement to fall through if there is code in
      it.. so enforcing an explicit break or goto makes perfect sense. Isn't it
      this same case that was the reason goto was kept in c# (just to move between
      cases in switch statements)?

      Pete


      Comment

      • Frank Eller [MVP]

        #18
        Re: C# Switch statement

        Hi Edward,

        "Edward Yang" <neo_in_matrix@ msn.com> schrieb im Newsbeitrag
        news:OIE6umSZDH A.2284@TK2MSFTN GP10.phx.gbl...[color=blue]
        >
        > "Frank Eller [MVP]" <webmaster@fran keller.de> wrote in message
        > news:%23G0$M7PZ DHA.1872@TK2MSF TNGP12.phx.gbl. ..[color=green]
        > > Hi Duncan,
        > >[color=darkred]
        > > > Is it possible to have ranges or patterns in a case ?[/color]
        > >
        > > No.
        > >[/color]
        >
        > It is a half yes. See Stan's reply in this thread.
        >[/color]

        Well, I agree that it's possible to execute a piece of code for more than
        one value, but that's not exactly what he meant ... because you have to
        write down the "case"-part, which you would not have to do if you could use
        a range like in VB.NET. But ok, let's call it a "half yes ..." :-)).

        REgards,

        --
        Frank Eller [.NET MVP]
        another blog about programming with all things related to .NET

        ..NET Developers Group Munich- www.munichdot.net


        Comment

        • Rob Tillie

          #19
          Re: C# Switch statement

          Deactivate Windows xp's? Deactivate Microsoft? :|
          You have certainly no affiliation with Microsoft, just remove it so nobody
          gets confused...

          GReetz,
          -- Rob.

          Edward Yang wrote:[color=blue]
          > So~~~ what do your team do?
          >
          > Edward[/color]


          Comment

          • Duncan .McNutt

            #20
            Re: C# Switch statement

            No :> I can put what i fluckin want. You dont have to read it.

            --

            Duncan McNutt
            Microsoft Product Deactivation Team
            --


            "Rob Tillie" <Rob.Tillie@stu dent.tul.edu> wrote in message
            news:ebq77FcZDH A.2256@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Deactivate Windows xp's? Deactivate Microsoft? :|
            > You have certainly no affiliation with Microsoft, just remove it so nobody
            > gets confused...
            >
            > GReetz,
            > -- Rob.
            >
            > Edward Yang wrote:[color=green]
            > > So~~~ what do your team do?
            > >
            > > Edward[/color]
            >
            >[/color]


            Comment

            • Duncan .McNutt

              #21
              Re: C# Switch statement

              You have no idea what I have affiliations with so bite me little man

              --

              Duncan McNutt
              Microsoft Product Deactivation Team
              --


              "Duncan .McNutt" <futmaster@127. 0.0.99> wrote in message
              news:uqjvfIcZDH A.4028@tk2msftn gp13.phx.gbl...[color=blue]
              > No :> I can put what i fluckin want. You dont have to read it.
              >
              > --
              >
              > Duncan McNutt
              > Microsoft Product Deactivation Team
              > --
              >
              >
              > "Rob Tillie" <Rob.Tillie@stu dent.tul.edu> wrote in message
              > news:ebq77FcZDH A.2256@TK2MSFTN GP10.phx.gbl...[color=green]
              > > Deactivate Windows xp's? Deactivate Microsoft? :|
              > > You have certainly no affiliation with Microsoft, just remove it so[/color][/color]
              nobody[color=blue][color=green]
              > > gets confused...
              > >
              > > GReetz,
              > > -- Rob.
              > >
              > > Edward Yang wrote:[color=darkred]
              > > > So~~~ what do your team do?
              > > >
              > > > Edward[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Eric Gunnerson [MS]

                #22
                Re: C# Switch statement

                It's a feature vs. complexity thing.. While we agree that there are cases
                where case ranges are useful, we don't think that their usefullness above
                what you get with a simple series of if statements outweighs the increase in
                complexity.

                --
                Eric Gunnerson

                Visit the C# product team at http://www.csharp.net
                Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                This posting is provided "AS IS" with no warranties, and confers no rights.
                "Duncan McNutt [BSDM]" <nutmaster@127. 0.0.66> wrote in message
                news:eNPUQtPZDH A.2352@TK2MSFTN GP12.phx.gbl...[color=blue]
                > Is it possible to have ranges or patterns in a case ?
                >
                > If not, why wasnt this designed in to make it easier instead of listing
                > every case value needed?
                >
                > --
                >
                > Duncan McNutt
                > Microsoft Product Deactivation Team
                > --
                >
                >
                >
                >[/color]


                Comment

                • Duncan .McNutt

                  #23
                  Re: C# Switch statement

                  Complexity for whom?

                  --

                  Duncan McNutt
                  Microsoft Product Deactivation Team
                  --


                  "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
                  news:ePk$macZDH A.2336@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > It's a feature vs. complexity thing.. While we agree that there are cases
                  > where case ranges are useful, we don't think that their usefullness above
                  > what you get with a simple series of if statements outweighs the increase[/color]
                  in[color=blue]
                  > complexity.
                  >
                  > --
                  > Eric Gunnerson
                  >
                  > Visit the C# product team at http://www.csharp.net
                  > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
                  >
                  > This posting is provided "AS IS" with no warranties, and confers no[/color]
                  rights.[color=blue]
                  > "Duncan McNutt [BSDM]" <nutmaster@127. 0.0.66> wrote in message
                  > news:eNPUQtPZDH A.2352@TK2MSFTN GP12.phx.gbl...[color=green]
                  > > Is it possible to have ranges or patterns in a case ?
                  > >
                  > > If not, why wasnt this designed in to make it easier instead of listing
                  > > every case value needed?
                  > >
                  > > --
                  > >
                  > > Duncan McNutt
                  > > Microsoft Product Deactivation Team
                  > > --
                  > >
                  > >
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Duncan McNutt [FTSE].

                    #24
                    Re: C# Switch statement

                    So a little thing like that that may aid code readability is not good but
                    adding crap like Lambada functions (anonymous methods) and partial types (
                    to aid splitting source files) that is good?

                    You have the BCLs make an entire pigs mess of VSplits and HSplits and you
                    say thats good?

                    Yet adding a simple thing like ranges into a case in the switch construct is
                    bad because you cant be assed to handle the complexity?

                    Pass the bong.


                    --

                    Duncan McNutt
                    Microsoft Product Deactivation Team
                    --


                    "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
                    news:ePk$macZDH A.2336@TK2MSFTN GP09.phx.gbl...[color=blue]
                    > It's a feature vs. complexity thing.. While we agree that there are cases
                    > where case ranges are useful, we don't think that their usefullness above
                    > what you get with a simple series of if statements outweighs the increase[/color]
                    in[color=blue]
                    > complexity.
                    >
                    > --
                    > Eric Gunnerson
                    >
                    > Visit the C# product team at http://www.csharp.net
                    > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
                    >
                    > This posting is provided "AS IS" with no warranties, and confers no[/color]
                    rights.[color=blue]
                    > "Duncan McNutt [BSDM]" <nutmaster@127. 0.0.66> wrote in message
                    > news:eNPUQtPZDH A.2352@TK2MSFTN GP12.phx.gbl...[color=green]
                    > > Is it possible to have ranges or patterns in a case ?
                    > >
                    > > If not, why wasnt this designed in to make it easier instead of listing
                    > > every case value needed?
                    > >
                    > > --
                    > >
                    > > Duncan McNutt
                    > > Microsoft Product Deactivation Team
                    > > --
                    > >
                    > >
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Duncan McNutt [ FTSE ]

                      #25
                      Re: C# Switch statement

                      Sign of a good person is admiting they made mistakes and learnt from them.

                      Hiding them is just weak.

                      --

                      Duncan McNutt
                      Microsoft Product Deactivation Team
                      --


                      "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
                      news:OIMkFDdZDH A.2344@TK2MSFTN GP09.phx.gbl...[color=blue][color=green]
                      > > What I said is nothing but TRUTH! I admit that I often make mistakes[/color][/color]
                      with[color=blue][color=green]
                      > > such code like
                      > >
                      > > <code>
                      > > if(var = 1)
                      > > </code>
                      > >
                      > > but I do NOT remember I ever made a mistake with case-break's.[/color]
                      >
                      > If you've been coding in C for 8 years you have made this mistake, even if[/color]
                      you don't remember or are too macho to admit it.[color=blue]
                      >[color=green]
                      > > Keep on pratising with C/C++, Michael![/color]
                      >
                      > Keep on pratising your spelling, Edward!
                      >
                      > --
                      > Michael Culley
                      >
                      >[/color]


                      Comment

                      • Edward Yang

                        #26
                        Re: C# Switch statement


                        "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
                        news:OIMkFDdZDH A.2344@TK2MSFTN GP09.phx.gbl...[color=blue][color=green]
                        > > What I said is nothing but TRUTH! I admit that I often make mistakes[/color][/color]
                        with[color=blue][color=green]
                        > > such code like
                        > >
                        > > <code>
                        > > if(var = 1)
                        > > </code>
                        > >
                        > > but I do NOT remember I ever made a mistake with case-break's.[/color]
                        >
                        > If you've been coding in C for 8 years you have made this mistake, even if[/color]
                        you don't remember or are too macho to admit it.

                        No, I never made such a mistake as far as I can remember.
                        [color=blue]
                        >[color=green]
                        > > Keep on pratising with C/C++, Michael![/color]
                        >
                        > Keep on pratising your spelling, Edward![/color]

                        Thanks for you encourage!
                        [color=blue]
                        >
                        > --
                        > Michael Culley
                        >
                        >[/color]


                        Comment

                        • Edward Yang

                          #27
                          Re: C# Switch statement


                          "Peter Vidler" <pvidler@gawab. com> wrote in message
                          news:fF40b.625$ tS2.104034@news fep2-win.server.ntli .net...[color=blue]
                          > Hi,
                          >[color=green]
                          > > but I do NOT remember I ever made a mistake with case-break's.[/color]
                          >
                          > Maybe YOU don't (doubtful), but many people do. Hence the design decision.[/color]

                          Pete, note that many != all!
                          [color=blue]
                          >
                          > You almost never want a case statement to fall through if there is code in
                          > it.. so enforcing an explicit break or goto makes perfect sense. Isn't it
                          > this same case that was the reason goto was kept in c# (just to move[/color]
                          between[color=blue]
                          > cases in switch statements)?
                          >
                          > Pete
                          >
                          >[/color]


                          Comment

                          • Austin Ehlers

                            #28
                            Re: C# Switch statement

                            On Mon, 18 Aug 2003 13:23:33 -0700, "Eric Gunnerson [MS]"
                            <ericgu@online. microsoft.com> wrote:
                            [color=blue]
                            >It's a feature vs. complexity thing.. While we agree that there are cases
                            >where case ranges are useful, we don't think that their usefullness above
                            >what you get with a simple series of if statements outweighs the increase in
                            >complexity.[/color]

                            I agree. However, what about adding syntax like this:

                            switch (num)
                            {
                            case 1 to 5:
                            //code
                            break;
                            }

                            and translate it to the appropriate IL as if it were written like:

                            switch (num)
                            {
                            case 1:
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                            //code
                            break;
                            }

                            As this allows increased readability and is easily maintainable?

                            Thanks,
                            Austin Ehlers

                            Comment

                            • Duncan  McNutt [FTSE]_

                              #29
                              Re: C# Switch statement

                              That is complex??


                              But theyre adding Lambada funtions (anonymous methods) and Partial types!!!

                              Thats even MORE complex and POINTLESS !!!

                              --

                              Duncan McNutt
                              Microsoft Product Deactivation Team
                              --


                              "Austin Ehlers" <the*remove*bor ed*me*guy16@hot mail.com> wrote in message
                              news:h043kv4cei 6lnn03r7lrdd71k fl9r7d83n@4ax.c om...[color=blue]
                              > On Mon, 18 Aug 2003 13:23:33 -0700, "Eric Gunnerson [MS]"
                              > <ericgu@online. microsoft.com> wrote:
                              >[color=green]
                              > >It's a feature vs. complexity thing.. While we agree that there are cases
                              > >where case ranges are useful, we don't think that their usefullness above
                              > >what you get with a simple series of if statements outweighs the increase[/color][/color]
                              in[color=blue][color=green]
                              > >complexity.[/color]
                              >
                              > I agree. However, what about adding syntax like this:
                              >
                              > switch (num)
                              > {
                              > case 1 to 5:
                              > //code
                              > break;
                              > }
                              >
                              > and translate it to the appropriate IL as if it were written like:
                              >
                              > switch (num)
                              > {
                              > case 1:
                              > case 2:
                              > case 3:
                              > case 4:
                              > case 5:
                              > //code
                              > break;
                              > }
                              >
                              > As this allows increased readability and is easily maintainable?
                              >
                              > Thanks,
                              > Austin Ehlers[/color]


                              Comment

                              • Peter Vidler

                                #30
                                Re: C# Switch statement

                                >>> but I do NOT remember I ever made a mistake with case-break's.[color=blue][color=green]
                                >>
                                >> Maybe YOU don't (doubtful), but many people do. Hence the design
                                >> decision.[/color]
                                >
                                > Pete, note that many != all![/color]

                                True, but language features should be designed around what many people will
                                find useful. Requiring a break or goto statement means you will get a
                                compile time error.. the alternative is an annoying-to-debug runtime
                                error -- which would you prefer? I would rather not debug a runtime error
                                just so the fraction of programmers who never make these mistakes will not
                                be so irritated with the switch statement (they can just use if-else if they
                                don't like it).

                                It should be noted that this is generation of a compile time error is
                                reminiscent of C++ (where strong typing generated compile time errors, and C
                                would only produce runtime errors). It's just taken one step further.

                                Pete


                                Comment

                                Working...