Connecting Tech Pros Worldwide Help | Site Map

[Flags] on enum declarations

Paul E Collins
Guest
 
Posts: n/a
#1: Nov 17 '05
The help file says that "bit fields [i.e. enums that have the Flags
attribute] can be combined using a bitwise OR operation, whereas
enumerated constants cannot", and yet this code works:

enum Blah { a, b, c }; // without [Flags]
....
Blah x = Blah.a | Blah.b;

So what is the purpose of the Flags attribute? I understand it's
potentially useful for reflection, so that (for example) the IDE's
IntelliSense knows which enums are meant to be combined, but does it
have no effect on compilation at all - and, if so, is that comment in
the help file incorrect?

P.


Dave
Guest
 
Posts: n/a
#2: Nov 17 '05

re: [Flags] on enum declarations


http://msdn.microsoft.com/library/de...classtopic.asp

If you read past the first line, you'll see that they don't mean "can't", they mean it's not intuitive. (Bad wording)

Also, read the example at the bottom. It shows how combined (or'ed) fields in an Enum without the FlagAttribute set will not
combine the flag values in the ToString() method. Instead, it will just display the value (which may be regarded as invalid).

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Paul E Collins" <find_my_real_address@CL4.org> wrote in message news:d50jbe$cer$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
> The help file says that "bit fields [i.e. enums that have the Flags attribute] can be combined using a bitwise OR operation,
> whereas enumerated constants cannot", and yet this code works:
>
> enum Blah { a, b, c }; // without [Flags]
> ...
> Blah x = Blah.a | Blah.b;
>
> So what is the purpose of the Flags attribute? I understand it's potentially useful for reflection, so that (for example) the
> IDE's IntelliSense knows which enums are meant to be combined, but does it have no effect on compilation at all - and, if so, is
> that comment in the help file incorrect?
>
> P.
>
>[/color]


Daniel O'Connell [C# MVP]
Guest
 
Posts: n/a
#3: Nov 17 '05

re: [Flags] on enum declarations



"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:d50jbe$cer$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
> The help file says that "bit fields [i.e. enums that have the Flags
> attribute] can be combined using a bitwise OR operation, whereas
> enumerated constants cannot", and yet this code works:
>
> enum Blah { a, b, c }; // without [Flags]
> ...
> Blah x = Blah.a | Blah.b;
>
> So what is the purpose of the Flags attribute? I understand it's
> potentially useful for reflection, so that (for example) the IDE's
> IntelliSense knows which enums are meant to be combined, but does it have
> no effect on compilation at all - and, if so, is that comment in the help
> file incorrect?
>[/color]

Within C# itself, the Flags attribute is ignored. IDEs, reflection, or other
tools might use it and other languages(or even C# in the future) might
enforce its use or emit warnings if you use bitwise operations on non-flags
enums. Remember, the framework is designed to support multiple languages.


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#4: Nov 17 '05

re: [Flags] on enum declarations


Flags indicates that the enum is intended to be used as a set of bit fields. The ony practical difference it makes is in the implementation of ToString on the enum. without the attribute it simply prints out teh symbolic equivelent of the value or the value uf there is no equiveleint. With teh attribute if it can't find the exact match it tries to oassembly the value from the bitwise orable fields and if successful prints out teh list of fields comma separated.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

The help file says that "bit fields [i.e. enums that have the Flags
attribute] can be combined using a bitwise OR operation, whereas
enumerated constants cannot", and yet this code works:

enum Blah { a, b, c }; // without [Flags]
...
Blah x = Blah.a | Blah.b;

So what is the purpose of the Flags attribute? I understand it's
potentially useful for reflection, so that (for example) the IDE's
IntelliSense knows which enums are meant to be combined, but does it
have no effect on compilation at all - and, if so, is that comment in
the help file incorrect?

P.

branco.medeiros@gmail.com
Guest
 
Posts: n/a
#5: Nov 17 '05

re: [Flags] on enum declarations


Paul E Collins wrote:[color=blue]
> The help file says that "bit fields [i.e. enums that have the Flags
> attribute] can be combined using a bitwise OR operation, whereas
> enumerated constants cannot", and yet this code works:
>
> enum Blah { a, b, c }; // without [Flags]
> ...
> Blah x = Blah.a | Blah.b;[/color]
<cut>

Really?

By the rules of the enum type, a, b and c would have the values 0, 1
and 2, respectivelly.

Then

Blah x = Blah.a | Blah.b;

would be x = 0 | 1, which is 1. Sure, if you test later if "a" is set
in x, you'll find that it is:

if ((x & a) == a) { ...

But then, "a" would be set here, also:

Blah y = Blah.b | Blah.c;
if ((y & a) == a) {

In other words, any number & 0 wil always return 0, and it would seem
that the "a" flag would always be set.

Now, if you had

enum Blah { a, b, c, d };

then the value of d would be 3. Now consider this:

Blah z = b | c;
if ((z & d) == d) {//... what gives?!?

Because the bit pattern of d (3 in binary is 11) is the same of OR'ing
together b and c (01 and 10 in binary, respectivelly), it would always
seem that d is set in a Blah var loaded with b and c (and
vice-versa)... Probably not what you'd expect.

It turns out that besides marking an enum with the Flags attribute so
the system considers it a bit mask, you must also choose the values of
the constants carefully, each one representing a specific bit pattern:

enum Blah { a=1, b=2, c=4, d=8, e=16 }; //and so on

Regards,

Branco.

Closed Thread