Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr.Tickle

    Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

    // Run this code and look for his obvious error by the compiler


    namespace FlagCheck
    {
    using System;

    [Flags]
    enum SomeFlags
    {
    None = 0,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    All = One | Two | Three | Four | Five | Six
    }

    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    SomeFlags f;

    f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
    supprise

    if ( (f & SomeFlags.One) > 0)
    {
    Console.WriteLi ne("flag one is set");
    }

    if ( (f & SomeFlags.Two) > 0)
    {
    Console.WriteLi ne("flag two is set");
    }

    if ( (f & SomeFlags.Three ) > 0)
    {
    Console.WriteLi ne("flag three is set");
    }

    if ( (f & SomeFlags.Four) > 0)
    {
    Console.WriteLi ne("flag four is set");
    }

    if ( (f & SomeFlags.Five) > 0)
    {
    Console.WriteLi ne("flag five is set");
    }

    if ( (f & SomeFlags.Six) > 0)
    {
    Console.WriteLi ne("flag six is set");
    }
    Console.ReadLin e();
    }
    }
    }


  • Alan Pretre

    #2
    Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

    "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
    news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=blue]
    > // Run this code and look for his obvious error by the compiler[/color]

    What you wrote and what you meant are two different things. The compiler is
    fine.

    You have:
    enum SomeFlags
    {
    None = 0, // 0000
    One, // 0001
    Two, // 0010
    Three, // 0011
    Four, // 0100
    Five, // 0101
    Six, // 0110
    All = One | Two | Three | Four | Five | Six // 0111
    };

    You meant:
    enum SomeFlags
    {
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    Five = 16,
    Six = 32,
    All = One | Two | Three | Four | Five | Six
    }

    Change your code and it works fine.

    -- Alan


    Comment

    • Stephen Martin

      #3
      Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

      Works fine for me.

      The out put is:

      flag four is set
      flag five is set
      flag six is set

      as expected.


      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
      news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=blue]
      > // Run this code and look for his obvious error by the compiler
      >
      >
      > namespace FlagCheck
      > {
      > using System;
      >
      > [Flags]
      > enum SomeFlags
      > {
      > None = 0,
      > One,
      > Two,
      > Three,
      > Four,
      > Five,
      > Six,
      > All = One | Two | Three | Four | Five | Six
      > }
      >
      > class Class1
      > {
      > [STAThread]
      > static void Main(string[] args)
      > {
      > SomeFlags f;
      >
      > f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
      > supprise
      >
      > if ( (f & SomeFlags.One) > 0)
      > {
      > Console.WriteLi ne("flag one is set");
      > }
      >
      > if ( (f & SomeFlags.Two) > 0)
      > {
      > Console.WriteLi ne("flag two is set");
      > }
      >
      > if ( (f & SomeFlags.Three ) > 0)
      > {
      > Console.WriteLi ne("flag three is set");
      > }
      >
      > if ( (f & SomeFlags.Four) > 0)
      > {
      > Console.WriteLi ne("flag four is set");
      > }
      >
      > if ( (f & SomeFlags.Five) > 0)
      > {
      > Console.WriteLi ne("flag five is set");
      > }
      >
      > if ( (f & SomeFlags.Six) > 0)
      > {
      > Console.WriteLi ne("flag six is set");
      > }
      > Console.ReadLin e();
      > }
      > }
      > }
      >
      >[/color]


      Comment

      • Mr.Tickle

        #4
        Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

        It should be powers of 2 by default for flags.

        Thats my point.


        "Alan Pretre" <no@spam> wrote in message
        news:#sbsc6mkDH A.4008@TK2MSFTN GP11.phx.gbl...[color=blue]
        > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
        > news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=green]
        > > // Run this code and look for his obvious error by the compiler[/color]
        >
        > What you wrote and what you meant are two different things. The compiler[/color]
        is[color=blue]
        > fine.
        >
        > You have:
        > enum SomeFlags
        > {
        > None = 0, // 0000
        > One, // 0001
        > Two, // 0010
        > Three, // 0011
        > Four, // 0100
        > Five, // 0101
        > Six, // 0110
        > All = One | Two | Three | Four | Five | Six // 0111
        > };
        >
        > You meant:
        > enum SomeFlags
        > {
        > None = 0,
        > One = 1,
        > Two = 2,
        > Three = 4,
        > Four = 8,
        > Five = 16,
        > Six = 32,
        > All = One | Two | Three | Four | Five | Six
        > }
        >
        > Change your code and it works fine.
        >
        > -- Alan
        >
        >[/color]


        Comment

        • Mr.Tickle

          #5
          Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

          WRONG, its a Flag, should be powers of 2. you ninney


          You expected wrong.


          "Stephen Martin" <smartin@remove this.emsoft.and this.ca> wrote in message
          news:u5GQQ7mkDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
          > Works fine for me.
          >
          > The out put is:
          >
          > flag four is set
          > flag five is set
          > flag six is set
          >
          > as expected.
          >
          >
          > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
          > news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=green]
          > > // Run this code and look for his obvious error by the compiler
          > >
          > >
          > > namespace FlagCheck
          > > {
          > > using System;
          > >
          > > [Flags]
          > > enum SomeFlags
          > > {
          > > None = 0,
          > > One,
          > > Two,
          > > Three,
          > > Four,
          > > Five,
          > > Six,
          > > All = One | Two | Three | Four | Five | Six
          > > }
          > >
          > > class Class1
          > > {
          > > [STAThread]
          > > static void Main(string[] args)
          > > {
          > > SomeFlags f;
          > >
          > > f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
          > > supprise
          > >
          > > if ( (f & SomeFlags.One) > 0)
          > > {
          > > Console.WriteLi ne("flag one is set");
          > > }
          > >
          > > if ( (f & SomeFlags.Two) > 0)
          > > {
          > > Console.WriteLi ne("flag two is set");
          > > }
          > >
          > > if ( (f & SomeFlags.Three ) > 0)
          > > {
          > > Console.WriteLi ne("flag three is set");
          > > }
          > >
          > > if ( (f & SomeFlags.Four) > 0)
          > > {
          > > Console.WriteLi ne("flag four is set");
          > > }
          > >
          > > if ( (f & SomeFlags.Five) > 0)
          > > {
          > > Console.WriteLi ne("flag five is set");
          > > }
          > >
          > > if ( (f & SomeFlags.Six) > 0)
          > > {
          > > Console.WriteLi ne("flag six is set");
          > > }
          > > Console.ReadLin e();
          > > }
          > > }
          > > }
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Mr.Tickle

            #6
            Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

            What i meant was bloody obvious and expected as its a FLAG, why the hell
            would I preform base 10 operations on a base 2 type (Flag)?
            "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
            news:uPpMV8mkDH A.1740@TK2MSFTN GP12.phx.gbl...[color=blue]
            > It should be powers of 2 by default for flags.
            >
            > Thats my point.
            >
            >
            > "Alan Pretre" <no@spam> wrote in message
            > news:#sbsc6mkDH A.4008@TK2MSFTN GP11.phx.gbl...[color=green]
            > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
            > > news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=darkred]
            > > > // Run this code and look for his obvious error by the compiler[/color]
            > >
            > > What you wrote and what you meant are two different things. The[/color][/color]
            compiler[color=blue]
            > is[color=green]
            > > fine.
            > >
            > > You have:
            > > enum SomeFlags
            > > {
            > > None = 0, // 0000
            > > One, // 0001
            > > Two, // 0010
            > > Three, // 0011
            > > Four, // 0100
            > > Five, // 0101
            > > Six, // 0110
            > > All = One | Two | Three | Four | Five | Six // 0111
            > > };
            > >
            > > You meant:
            > > enum SomeFlags
            > > {
            > > None = 0,
            > > One = 1,
            > > Two = 2,
            > > Three = 4,
            > > Four = 8,
            > > Five = 16,
            > > Six = 32,
            > > All = One | Two | Three | Four | Five | Six
            > > }
            > >
            > > Change your code and it works fine.
            > >
            > > -- Alan
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Alan Pretre

              #7
              Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

              "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
              news:eKxeH8mkDH A.2432@TK2MSFTN GP10.phx.gbl...[color=blue]
              > WRONG, its a Flag, should be powers of 2. you ninney[/color]

              Looks like someone needs a lesson about attributes. What a hoot. As if
              attributes morph the language.

              -- Alan


              Comment

              • Mr.Tickle

                #8
                Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                They influence the operators , why not the enum default values.

                So in a way they do MORPH it. fagot.ur a hoot.


                "Alan Pretre" <no@spam> wrote in message
                news:#22Sd#mkDH A.1488@TK2MSFTN GP12.phx.gbl...[color=blue]
                > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                > news:eKxeH8mkDH A.2432@TK2MSFTN GP10.phx.gbl...[color=green]
                > > WRONG, its a Flag, should be powers of 2. you ninney[/color]
                >
                > Looks like someone needs a lesson about attributes. What a hoot. As if
                > attributes morph the language.
                >
                > -- Alan
                >
                >[/color]


                Comment

                • Patrick Steele [MVP]

                  #9
                  Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                  In article <eKxeH8mkDHA.24 32@TK2MSFTNGP10 .phx.gbl>, MrTickle@mrmen. com
                  says...[color=blue]
                  > WRONG, its a Flag, should be powers of 2[/color]

                  Check the documentation for the FlagsAttribute class:

                  "Indicates that an enumeration can be treated as a bit field"

                  It says it "can be treated" as a bit field. It does not mean the
                  enumeration "will be defined" as a bitfield.

                  --
                  Patrick Steele
                  Microsoft .NET MVP

                  Comment

                  • Mr.Tickle

                    #10
                    Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                    Thats my point, it should be. There is no reason to have it otherwise.

                    Logic dictates that it should be powers of 2, nothing else.


                    "Patrick Steele [MVP]" <patrick@mvps.o rg> wrote in message
                    news:MPG.19f5ee bfc6dd213a98982 4@msnews.micros oft.com...[color=blue]
                    > In article <eKxeH8mkDHA.24 32@TK2MSFTNGP10 .phx.gbl>, MrTickle@mrmen. com
                    > says...[color=green]
                    > > WRONG, its a Flag, should be powers of 2[/color]
                    >
                    > Check the documentation for the FlagsAttribute class:
                    >
                    > "Indicates that an enumeration can be treated as a bit field"
                    >
                    > It says it "can be treated" as a bit field. It does not mean the
                    > enumeration "will be defined" as a bitfield.
                    >
                    > --
                    > Patrick Steele
                    > Microsoft .NET MVP
                    > http://weblogs.asp.net/psteele[/color]


                    Comment

                    • Alan Pretre

                      #11
                      Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      news:u0Tug$mkDH A.2244@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > They influence the operators , why not the enum default values.
                      > So in a way they do MORPH it. fagot.ur a hoot.[/color]

                      Why don't you RTM and see what it does. Does it say that the FlagsAttribute
                      changes the way enum constants are defined? Hmmm? No it causes the
                      compiler to relax the restrictions on the bitwise OR operator, which would
                      otherwise not be allowed on the enum members.

                      -- Alan


                      Comment

                      • Mr.Tickle

                        #12
                        Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                        Sure you can override it if you want other values, just like normal
                        behaviour today. but having an option to have it enumerate in powers of 2
                        would be handy as in my example. Isnt that the point of Enums, to
                        enumerate, in this case it enumerats incorrectly for bitfields on a base 2
                        system.



                        "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                        news:#OzLADnkDH A.2772@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > Thats my point, it should be. There is no reason to have it otherwise.
                        >
                        > Logic dictates that it should be powers of 2, nothing else.
                        >
                        >
                        > "Patrick Steele [MVP]" <patrick@mvps.o rg> wrote in message
                        > news:MPG.19f5ee bfc6dd213a98982 4@msnews.micros oft.com...[color=green]
                        > > In article <eKxeH8mkDHA.24 32@TK2MSFTNGP10 .phx.gbl>, MrTickle@mrmen. com
                        > > says...[color=darkred]
                        > > > WRONG, its a Flag, should be powers of 2[/color]
                        > >
                        > > Check the documentation for the FlagsAttribute class:
                        > >
                        > > "Indicates that an enumeration can be treated as a bit field"
                        > >
                        > > It says it "can be treated" as a bit field. It does not mean the
                        > > enumeration "will be defined" as a bitfield.
                        > >
                        > > --
                        > > Patrick Steele
                        > > Microsoft .NET MVP
                        > > http://weblogs.asp.net/psteele[/color]
                        >
                        >[/color]


                        Comment

                        • Mr.Tickle

                          #13
                          Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                          Sure, RTM RTM blah, suppose you say "Linux owns j000, go rtfm l0ser!"

                          u sad fuckk. My issue is, why cant it not have powers of 2 for default
                          enumerations on bitfields where it would be useful, for gawds sakes remove
                          yer head from yer arsee and think beyond the manual.

                          "Alan Pretre" <no@spam> wrote in message
                          news:eF50hDnkDH A.3316@TK2MSFTN GP11.phx.gbl...[color=blue]
                          > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                          > news:u0Tug$mkDH A.2244@TK2MSFTN GP12.phx.gbl...[color=green]
                          > > They influence the operators , why not the enum default values.
                          > > So in a way they do MORPH it. fagot.ur a hoot.[/color]
                          >
                          > Why don't you RTM and see what it does. Does it say that the[/color]
                          FlagsAttribute[color=blue]
                          > changes the way enum constants are defined? Hmmm? No it causes the
                          > compiler to relax the restrictions on the bitwise OR operator, which would
                          > otherwise not be allowed on the enum members.
                          >
                          > -- Alan
                          >
                          >[/color]


                          Comment

                          • Mr.Tickle

                            #14
                            Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                            I never said it changes the way theyre defined, thats what im asking for.

                            Do you ever think BEYOND the manual? Guess you are one of those backroom
                            uncreative programmers that just copies from a manual and CANT THINK FOR
                            YERSELF OR FURTHER.

                            "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                            news:upNpXFnkDH A.2268@TK2MSFTN GP12.phx.gbl...[color=blue]
                            > Sure, RTM RTM blah, suppose you say "Linux owns j000, go rtfm l0ser!"
                            >
                            > u sad fuckk. My issue is, why cant it not have powers of 2 for default
                            > enumerations on bitfields where it would be useful, for gawds sakes remove
                            > yer head from yer arsee and think beyond the manual.
                            >
                            > "Alan Pretre" <no@spam> wrote in message
                            > news:eF50hDnkDH A.3316@TK2MSFTN GP11.phx.gbl...[color=green]
                            > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                            > > news:u0Tug$mkDH A.2244@TK2MSFTN GP12.phx.gbl...[color=darkred]
                            > > > They influence the operators , why not the enum default values.
                            > > > So in a way they do MORPH it. fagot.ur a hoot.[/color]
                            > >
                            > > Why don't you RTM and see what it does. Does it say that the[/color]
                            > FlagsAttribute[color=green]
                            > > changes the way enum constants are defined? Hmmm? No it causes the
                            > > compiler to relax the restrictions on the bitwise OR operator, which[/color][/color]
                            would[color=blue][color=green]
                            > > otherwise not be allowed on the enum members.
                            > >
                            > > -- Alan
                            > >
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            • Mr.Tickle

                              #15
                              Re: Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

                              Would it not be useful to be able specify an increment like powers of 2 for
                              bitfields, or any other increment somehow? Why must it always be one, I know
                              what the damnn manual says, dont quote me on that crap, I am thinking OUT OF
                              THE BOX (something which alot of you lot have yet to experience)



                              "Patrick Steele [MVP]" <patrick@mvps.o rg> wrote in message
                              news:MPG.19f5ee bfc6dd213a98982 4@msnews.micros oft.com...[color=blue]
                              > In article <eKxeH8mkDHA.24 32@TK2MSFTNGP10 .phx.gbl>, MrTickle@mrmen. com
                              > says...[color=green]
                              > > WRONG, its a Flag, should be powers of 2[/color]
                              >
                              > Check the documentation for the FlagsAttribute class:
                              >
                              > "Indicates that an enumeration can be treated as a bit field"
                              >
                              > It says it "can be treated" as a bit field. It does not mean the
                              > enumeration "will be defined" as a bitfield.
                              >
                              > --
                              > Patrick Steele
                              > Microsoft .NET MVP
                              > http://weblogs.asp.net/psteele[/color]


                              Comment

                              Working...