Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Useful thingy

Useful thingy

Scheduled Pinned Locked Moved Clever Code
csharpdatabasecomquestion
5 Posts 3 Posters 22 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    I have just noticed that in the .NET 4.0 the Enum.HasFlag(Enum)[^] method was introduced. Not much clever from my side, but I rarely put a dot after enum's member so I could notice the new feature. :thumbsup:

    Greetings - Jacek

    D B 2 Replies Last reply
    0
    • L Lutoslaw

      I have just noticed that in the .NET 4.0 the Enum.HasFlag(Enum)[^] method was introduced. Not much clever from my side, but I rarely put a dot after enum's member so I could notice the new feature. :thumbsup:

      Greetings - Jacek

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      [deleted - made a boo boo] :doh:

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


      Folding Stats: Team CodeProject

      modified on Thursday, July 7, 2011 3:15 PM

      L 1 Reply Last reply
      0
      • D DaveAuld

        [deleted - made a boo boo] :doh:

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        modified on Thursday, July 7, 2011 3:15 PM

        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        This is not how HasFlag works... But of course it is easy to recreate with a simple AND.

        Greetings - Jacek

        D 1 Reply Last reply
        0
        • L Lutoslaw

          This is not how HasFlag works... But of course it is easy to recreate with a simple AND.

          Greetings - Jacek

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #4

          Your right......I should have read the docs :doh:

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          1 Reply Last reply
          0
          • L Lutoslaw

            I have just noticed that in the .NET 4.0 the Enum.HasFlag(Enum)[^] method was introduced. Not much clever from my side, but I rarely put a dot after enum's member so I could notice the new feature. :thumbsup:

            Greetings - Jacek

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Hi Jacek, Interesting, I thought I had been using 'HasFlag on the MouseButtons Enum in Windows Forms previous to .NET 4.0. Also interesting about 'HasFlag is that it will work even on Enumerations that are not decorated with the [Flags] Attribute, while the MS documentation implies, in my reading, that it only works with same: "The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute."

                // from the MS doc for Enum.HasFlag
                // no \[Flags\] attribute !     
                public enum DinnerItems
                {
                    None = 0,
                    Entree = 1,
                    Appetizer = 2,
                    Side = 4,
                    Dessert = 8,
                    Beverage = 16,
                    BarBeverage = 32
                }
                
                // execute this somewhere in your code    
                DinnerItems thisMeal;
            
                // This will also compile without error in .NET 4.0 !
                // DinnerItems thisMeal = new DinnerItems();
            
                thisMeal = DinnerItems.Dessert;
            
                if (thisMeal == DinnerItems.Dessert) MessageBox.Show("woof woof");
            
                if(thisMeal.HasFlag(DinnerItems.Dessert)) MessageBox.Show("meow");
            

            The comment on the MSDN page you linked to by a "community member" that using 'HasFlag is "1000 times slower" than the "standard method" is kind of scary. best, Bill

            "Reason is the natural order of truth; but imagination is the organ of meaning." C.S. Lewis

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups