Flags Enums
-
This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:
[Flags]
public enum ThisIsAnEnum
{
None,
FirstFlag,
SecondFlag,
ThirdFlag,
FourthFlag
}How do I test whether a specific value is used? Is this the proper way?
ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
if (enumValue & ThisIsAnEnum.SecondFlag != 0)
{
// Do something...
}Or is there some method like
Flags.Match()
, but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:[Flags]
public enum ThisIsAnEnum
{
None = 0,
FirstFlag = 1,
SecondFlag = 2,
ThirdFlag = 4,
FourthFlag = 8
}If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker
The earth is not dying. it is being killed...
-
This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:
[Flags]
public enum ThisIsAnEnum
{
None,
FirstFlag,
SecondFlag,
ThirdFlag,
FourthFlag
}How do I test whether a specific value is used? Is this the proper way?
ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
if (enumValue & ThisIsAnEnum.SecondFlag != 0)
{
// Do something...
}Or is there some method like
Flags.Match()
, but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:[Flags]
public enum ThisIsAnEnum
{
None = 0,
FirstFlag = 1,
SecondFlag = 2,
ThirdFlag = 4,
FourthFlag = 8
}If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker
The earth is not dying. it is being killed...
Daniël Pelsmaeker wrote: Is this the proper way? ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;if (enumValue & ThisIsAnEnum.SecondFlag != 0){ // Do something...} Close:
ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
if ((enumValue & ThisIsAnEnum.SecondFlag) != 0)
{
// Do Something...
}Putting the bitwise
&
statement in its own pair of parens is necessary due to operator precedence.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Daniël Pelsmaeker wrote: Is this the proper way? ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;if (enumValue & ThisIsAnEnum.SecondFlag != 0){ // Do something...} Close:
ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
if ((enumValue & ThisIsAnEnum.SecondFlag) != 0)
{
// Do Something...
}Putting the bitwise
&
statement in its own pair of parens is necessary due to operator precedence.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thank you very much. - Daniël Pelsmaeker
"The secret of a successful restaurant is sharp knives." - George Orwell [pseud. of Eric Arthur Blair]
-
This is propably a question from which everyone should know the answer, but I want to ask it anyway. ;P If I have an enum, like this:
[Flags]
public enum ThisIsAnEnum
{
None,
FirstFlag,
SecondFlag,
ThirdFlag,
FourthFlag
}How do I test whether a specific value is used? Is this the proper way?
ThisIsAnEnum enumValue = ThisIsAnEnum.SecondFlag | ThisIsAnEnum.FourthFlag;
if (enumValue & ThisIsAnEnum.SecondFlag != 0)
{
// Do something...
}Or is there some method like
Flags.Match()
, but for a bitfield value? And why do I have to add the values of the enum to it's members? Like this:[Flags]
public enum ThisIsAnEnum
{
None = 0,
FirstFlag = 1,
SecondFlag = 2,
ThirdFlag = 4,
FourthFlag = 8
}If I don't, their values will be 0, 1, 2, 3, 4, 5, 6..., and not 0, 1, 2, 4, 8, 16... . Thanks in advance, - Daniël Pelsmaeker
The earth is not dying. it is being killed...
[Flags] is merely an attribute that's put on enums - it doesn't change the compiler behavior. We don't automatically switch to powers of two because people often want to define combinations of flags, and what was going on would be much less clear if that was the case. Ditto for looking at the numeric values someplace and having to translate back to the identifiers.
-
[Flags] is merely an attribute that's put on enums - it doesn't change the compiler behavior. We don't automatically switch to powers of two because people often want to define combinations of flags, and what was going on would be much less clear if that was the case. Ditto for looking at the numeric values someplace and having to translate back to the identifiers.
Okay, but what use is the
Flagsattribute
then? If I'm not mistaken, I can add enum values together using|
, even when I didn't set theFlagsattribute
. -
Okay, but what use is the
Flagsattribute
then? If I'm not mistaken, I can add enum values together using|
, even when I didn't set theFlagsattribute
.[Flags] isn't really a compiler thing. It's used by object browsers to tell people what kind of enumeration it is, and Enum.ToString() bases its behavior on whether [Flags] is present.