Questions tagged [enum-flags]

167 questions
162
votes
19 answers

How to iterate over values of an Enum having flags?

If I have a variable holding a flags enum, can I somehow iterate over the single-bit values in that specific variable? Or do I have to use Enum.GetValues to iterate over the entire enum and check which ones are set?
user502255
135
votes
7 answers

Why are flag enums usually defined with hexadecimal values

A lot of times I see flag enum declarations that use hexadecimal values. For example: [Flags] public enum MyEnum { None = 0x0, Flag1 = 0x1, Flag2 = 0x2, Flag3 = 0x4, Flag4 = 0x8, Flag5 = 0x10 } When I declare an enum, I…
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
47
votes
5 answers

How to set all bits of enum flag

I wonder a generic way for setting all bits of enum flag to 1. I just would like to have an enum which returns for all comparisons, regardless of other enums. And this code works; [Flags] public enum SomeRightEnum : uint { CanDoNothing = 0, …
AFgone
  • 1,172
  • 4
  • 16
  • 31
37
votes
6 answers

Model Bind List of Enum Flags

I have a grid of Enum Flags in which each record is a row of checkboxes to determine that record's flag values. This is a list of notifications that the system offers and the user can pick (for each one) how they want them delivered: [Flag] public…
Rikon
  • 2,688
  • 3
  • 22
  • 32
36
votes
6 answers

Flags enum & bitwise operations vs. “string of bits”

A fellow developer suggested we store a selection of days of the week as 7-character string of 1’s and 0’s, i.e. “1000100” for Monday and Friday. I preferred (and strongly suggested) a solution with a Flags enum and bitwise operations, I think it's…
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
34
votes
2 answers

Setting multiple enum flags in XAML

Is there any way to set multiple enum flags (that are traditionally separated by | in codebehind) in XAML? I tried something like: but that didn't work.
K Mehta
  • 10,323
  • 4
  • 46
  • 76
34
votes
7 answers

HasFlags always returns true for None (0) value in enum

This is the enum definition: [Flags] enum Animals { None = 0, Dog = 1, Cat = 2, Horse = 4, Zebra = 8, } Now, given the following code, why does the HasFlag method return true for the value Animals.None? Animals myAnimals =…
Henrik Söderlund
  • 4,286
  • 3
  • 26
  • 26
31
votes
2 answers

FlagsAttribute Enum problems

So I'm building an MSNP (windows live messenger) client. And I've got this list of capabilities public enum UserCapabilities : long { None = 0, MobileOnline = 1 << 0, MSN8User = 1 << 1, RendersGif = 1 << 2, .... MsgrVersion7…
NoPyGod
  • 4,905
  • 3
  • 44
  • 72
20
votes
5 answers

In pursuit of a better bitflag enum

Okay, so we're at C++ 17 and there still isn't a satisfactory answer to a really great bitflags interface in C++. We have enum which bleed their member values into the enclosing scope, but do implicitly convert to their underlying type, so can be…
Mordachai
  • 9,412
  • 6
  • 60
  • 112
17
votes
11 answers

C#, Flags Enum, Generic function to look for a flag

I'd like one general purpose function that could be used with any Flags style enum to see if a flag exists. This doesn't compile, but if anyone has a suggestion, I'd appreciate it. public static Boolean IsEnumFlagPresent(T value,T lookingForFlag)…
jeff
  • 3,269
  • 3
  • 28
  • 45
17
votes
5 answers

Should "or" work with .Net4 Hasflags: enum.HasFlag(AccessRights.Read | AccessRights.Write)

I am trying out the new HasFlags features, and was wondering if the following should work: enum.HasFlag(AccessRights.Read | AccessRights.Write) ... because it doesn't seem to... DBAccessRights rights =…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
14
votes
1 answer

How to get the numeric value from a flags enum?

Possible Duplicate: Enums returning int value How to get the numeric value from the Enum? THIS IS NOT A DUPLICATE OF EITHER OF THESE, THANKS FOR READING THE QUESTION MODS Suppose I have a number of my flags enum items selected: [Flags] public…
DaveDev
  • 41,155
  • 72
  • 223
  • 385
10
votes
4 answers

Best practice for checking for an enum flag

I noticed these two patterns for checking for an enum flag: [Flags] public enum PurchaseType { None = 0, SalePrice = 2, RegularPrice = 4, Clearance = 8, CreditCard = 16 } public void Test() { PurchaseType type =…
Mohammed Ali
  • 1,027
  • 1
  • 9
  • 16
9
votes
1 answer

Using checkboxes to PostBack Enum with Flags

I have an enum property and I am trying to set its value through checkboxes. The enum is flagged and when the user selects multiple options I expect the property to have all the selected flags concatenated. I tried adding a checkbox for each enum…
Lordbalmon
  • 1,634
  • 1
  • 14
  • 31
8
votes
8 answers

How do I get all possible combinations of an enum ( Flags )

[Flags] public enum MyEnum { None = 0, Setting1 = (1 << 1), Setting2 = (1 << 2), Setting3 = (1 << 3), Setting4 = (1 << 4), } I need to be able to somehow loop over every posible setting and pass the settings combination to a…
EKS
  • 5,543
  • 6
  • 44
  • 60
1
2 3
11 12