I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Why do we need a [Flags] attribute on enums?

Enums are a great tool. They give meaning to meaningless numbers.

I love using them as flags since it couldn't be simpler. So i always used the [Flags] attribute on my enums

But why do we need that attribute? You can bitwise non [Flags]'d enums just the same.

The difference lies in the Enum.ToString() method. If your enum has the [Flags] attribute set then the ToString() will return a

CSV separated list of bitwised enum values. If there's no [Flags] attribute ToString() will return a number for every bitwised value.

 

As always it's best ilustrated with an example:

[Flags]
enum StateWithFlags
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4
}

enum StateWithNoFlags
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4            
}

for (int enumValue = 0; enumValue <= 4; enumValue++)
    Console.WriteLine("StateWithFlags: " + enumValue.ToString() + " " + ((StateWithFlags)enumValue).ToString());
/*
this returns:
0 None
1 Read
2 Write
3 Read, Write
4 Delete
*/


for (int enumValue = 0; enumValue <= 4; enumValue++)
    Console.WriteLine("StateWithNoFlags: " + enumValue.ToString() + " " + ((StateWithNoFlags)enumValue).ToString());
/*
this returns:
0 None
1 Read
2 Write
3 3
4 Delete
*/

 

So we see that the flags attribute is quite handy when trying to simply display the combination of our choices.

Print | posted on Friday, January 12, 2007 5:40 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: Why do we need a [Flags] attribute on enums?

Should a comma denote the logical AND? ;)
1/12/2007 8:27 PM | ML
Gravatar

# re: Why do we need a [Flags] attribute on enums?

AND, OR, XOR, NOT... it's all the same. :)))
1/12/2007 8:29 PM | Mladen
Gravatar

# re: Why do we need a [Flags] attribute on enums?

Wow! I did not know that. Quite cool. Now if out could just override enums .ToString(), that would make me happy. :)
1/12/2007 9:42 PM | David
Gravatar

# re: Why do we need a [Flags] attribute on enums?

no can do... believe me i tried :)

the closest i've come to it is this:
http://blogs.crankygoblin.com/blogs/geoff.appleby/archive/2004/11/18/32533.aspx
1/12/2007 9:57 PM | Mladen
Gravatar

# re: Why do we need a [Flags] attribute on enums?

I have tried it too, I know it's impossible. :) The workaarround is too messy imho. Nice, but I wouldn't use it.
1/13/2007 10:14 AM | David
Gravatar

# re: Why do we need a [Flags] attribute on enums?

I use to decorate enum values with Description attribute and get description from there.
1/31/2007 10:40 PM | Miha Markic
Gravatar

# re: Why do we need a [Flags] attribute on enums?

Flags are to represent your country
7/18/2007 2:41 AM | Georgia
Gravatar

# re: Why do we need a [Flags] attribute on enums?

Very good post - very interesting. I can see myself using this a lot. I've just moved from VB to C#. Enums are under-used in my experience.
11/20/2009 9:35 PM | peter
Gravatar

# re: Why do we need a [Flags] attribute on enums?

why do every state NEED a state flag
4/6/2010 2:28 AM | lex!
Gravatar

# re: Why do we need a [Flags] attribute on enums?

Look here :
http://weblogs.asp.net/wim/archive/2004/04/07/109095.aspx
4/25/2010 9:16 AM | Valera
Gravatar

# re: Why do we need a [Flags] attribute on enums?

all i want to know why we need a FLAG
4/29/2010 11:22 PM | ella
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET