Questions tagged [enum-flags]

167 questions
8
votes
4 answers

Convert some bool properties to a flags enum

I need to convert a legacy class with 3 bool properties to a flag enum. I know that at least one of those properties is true. [Flags] public enum FlagEnum { EnumValue1 = 1, EnumValue2 = 2, EnumValue3 = 4 } public class LegacyClass { …
giammin
  • 18,620
  • 8
  • 71
  • 89
8
votes
3 answers

C# Enums - Check Flags against a Mask

I have the following enum flags: [Flags] private enum MemoryProtection: uint { None = 0x000, NoAccess = 0x001, ReadOnly = 0x002, ReadWrite = 0x004, WriteCopy = 0x008, Execute …
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
8
votes
3 answers

check if an enum has any flags in common

Consider I have this extension method: public static bool HasAnyFlagInCommon(this System.Enum type, Enum value) { var flags = value.ToString().Split(new string[] { ", " }, StringSplitOptions.None); …
Erpel
  • 150
  • 1
  • 8
7
votes
1 answer

HasFlag with a generic enum?

I am just starting with Generics in C# but have run into a problem early on, how can I call .HasFlag() on a generic Enum? public class Example where TEnum : struct { } How can I add the [Flags] attribute to it?
DevDave
  • 6,700
  • 12
  • 65
  • 99
6
votes
2 answers

Check if an enum flag contains a certain flag value

In C# (using Unity working on a game) I have an enum with the [Flag] attribute. I have instantiated it twice. I would like a way to compare the two enums. Specifically if enum A (which will have multiple flags) contains a flag from enum B (which…
George Barron
  • 99
  • 2
  • 3
  • 10
6
votes
5 answers

Random value from Flags enum

Say I have a function that accepts an enum decorated with the Flags attribute. If the value of the enum is a combination of more than one of the enum elements how can I extract one of those elements at random? I have the following but it seems there…
Chris Porter
  • 2,449
  • 2
  • 24
  • 26
6
votes
2 answers

Haxe - sending enum as flags to a function

I'm just trying to convert my code from C# to Haxe NME. I use enums as flags. [Flags] enum State { StateOne = 1, StateTwo = 2, StateThree = 4 } And use it if (someObj.HasState(State.StateOne | State.StateTwo)) { // Contains…
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
5
votes
4 answers

Append Enum Flags to a Parameter in a Loop (Bitwise Appending)

In C#, I am trying to "add" values to a parameter that accepts enumerated flags. I can do it on one line with a bitwise operator "|", but I can't seem append to the parameter in a loop. I have the following Enum specified as Flags. [Flags] public…
bigmac
  • 2,553
  • 6
  • 38
  • 61
5
votes
3 answers

Check if an enum contains more than one flag

I am trying to check if an "enum instance" contains more than one flag. [Flags] public enum Foo { Bar = 1, Far = 2 } var multiState = Foo.Bar | Foo.Far; MoreThanOneFlag(multiState); // True var singleState =…
Twenty
  • 5,234
  • 4
  • 32
  • 67
5
votes
4 answers

Using C# Enum Flags To Return A Set Of Values Based On Their Bit-Wise OR'ed Values

I understand that you can configure C# enum flags in this way: [Flags] public enum MyEnum { Unknown = 0, Type1 = 1, Type2 = 2, Type3 = 4, Type4 = 8, Type5 = 16 } And once this is…
Rick
  • 699
  • 1
  • 6
  • 17
5
votes
3 answers

How to convert from a string to a Flags enum format in C#

I have this enum: [Flags] public enum Countries { None = 0, USA = 1, Mexico = 2, Canada = 4, Brazil = 8, Chile = 16 } I receive in input strings like these: string selectedCountries = "Usa, Brazil, Chile"; how…
Paul_T
  • 55
  • 5
5
votes
2 answers

C# extension method to check if an enumeration has a flag set

I want to make an extension method to check if an enumeration has a flag. DaysOfWeek workDays = DaysOfWeek.Monday | DaysOfWeek.Tuesday | DaysOfWeek.Wednesday; // instead of this: if ((workDays & DaysOfWeek.Monday) == DaysOfWeek.Monday) ... // I…
Marlon
  • 19,924
  • 12
  • 70
  • 101
5
votes
5 answers

What happens if we declare [Flags] enum in order?

I have came across this question: What does the [Flags] Enum Attribute mean in C#? And one thing I have been wondering, using the accepted answer's example, what will happen if I declare: [Flags] public enum MyColors { Yellow = 1, Green =…
Tan Jia Ming
  • 435
  • 1
  • 6
  • 22
5
votes
1 answer

Unexpected behavior between [Flags] enum : long vs [Flags] enum : ulong

Compiles but shouldn't [Flags] enum TransactionData : long // 64 bits. Last bit is sign bit, but I'm putting data there { None = 0, Color1 = 1 << 63, } Errors but shouldn't [Flags] enum TransactionData : ulong // 64 bits. No sign bit. …
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
5
votes
3 answers

Scala equivalent of flags

Isn't there an equivalent for flags in Scala ? Something like : val flags = Something | SomeOtherThing I guess I could do a class for each flag set and have a bunch of booleans but what about syntactic sugar?
Théo Winterhalter
  • 4,908
  • 2
  • 18
  • 34
1
2
3
11 12