enum-flags

Check if an enum flag contains a certain flag value

柔情痞子 提交于 2021-02-04 16:32:07
问题 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 will only ever be assigned a single flag). I am not trying to compare a single instantiated enum to a single flag (this has been answered multiple times). I suspect I could do this by dumping values with GetValue and comparing those values on a

Check if an enum contains more than one flag [duplicate]

和自甴很熟 提交于 2021-01-03 22:15:19
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Test that only a single bit is set in Flags Enum [duplicate] (1 answer) Closed 29 days ago . 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 = Foo.Bar; MoreThanOneFlag(singleState); // False Additionally I really don't wanna use

Check if an enum contains more than one flag [duplicate]

此生再无相见时 提交于 2021-01-03 22:12:33
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Test that only a single bit is set in Flags Enum [duplicate] (1 answer) Closed 29 days ago . 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 = Foo.Bar; MoreThanOneFlag(singleState); // False Additionally I really don't wanna use

Check if an enum contains more than one flag [duplicate]

邮差的信 提交于 2021-01-03 22:10:27
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Test that only a single bit is set in Flags Enum [duplicate] (1 answer) Closed 29 days ago . 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 = Foo.Bar; MoreThanOneFlag(singleState); // False Additionally I really don't wanna use

Getting common flags enum value in c#

让人想犯罪 __ 提交于 2020-12-26 12:26:28
问题 Say I have the below enum [Flags] enum Letters { A = 1, B = 2, C = 4, D = 8, E = 16, F = 32, AB = A | B, All = A | B | C, } If I have the variables: var s = Letters.A | Letters.B | Letters.D; var p = Letters.A | Letters.C | Letters.D | Letters.E; What I want is to get the common values between these two enums so in this case it should be A | D . Can some one please tell me how I can achieve this. Thanks 回答1: You can get that using the binary & (and) operator: var s = Letters.A | Letters.B |

Test that only a single bit is set in Flags Enum [duplicate]

∥☆過路亽.° 提交于 2020-12-23 08:31:09
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Closed 18 days ago . So I have a flags Enum public Enum test { test1 = 1, test2 = 2, test3 = 4, etc. } How can I test that one bit, and only one bit is set? I've 100% done this before but my mind is not working this am! 回答1: To check that only a single bit is set in a number, the number must (by definition) be a power of two. As such, you can use the following to test: int intVal = ((int

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

瘦欲@ 提交于 2019-12-30 04:05:48
问题 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 = (DBAccessRights)permission.PermissionFlags; if (rights.HasFlag(DBAccessRights.WikiMode)) { // works } if (rights.HasFlag(DBAccessRights.WikiMode | DBAccessRights.CreateNew)) { // Doesn't work } DBAccessRights flags = DBAccessRights.WikiMode | DBAccessRights.CreateNew; if (rights.HasFlag(flags))

Using a OR'ed Enum in a custom UITypeEditor

回眸只為那壹抹淺笑 提交于 2019-12-25 06:15:50
问题 I have a property on a custom control I have written that is an Flag based Enum. I created my own custom control to edit it in a way that makes logical sense and called it from my own UITypeEditor. The problem is that Visual Studio generates an error when the value I attempt to store is a combination of the flags it tells me the value is invalid. Example: public enum TrayModes { SingleUnit = 0x01 , Tray = 0x02 , Poll = 0x04 , Trigger = 0x08 }; If the value I want to save is SingleUnit |

Convert some bool properties to a flags enum

那年仲夏 提交于 2019-12-22 04:54:09
问题 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 { public bool PropA { get; set; } public bool PropB { get; set; } public bool PropC { get; set; } } public class DtoClass { public FlagEnum FlagEnum { get; set; } public DtoClass(LegacyClass legacyClass) { if (!legacyClass.PropA && !legacyClass.PropB && !legacyClass

How to iterate over values of an Enum having flags?

最后都变了- 提交于 2019-12-17 00:55:29
问题 If I have a variable holding a flags enum, can I somehow iterate over the 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? 回答1: static IEnumerable<Enum> GetFlags(Enum input) { foreach (Enum value in Enum.GetValues(input.GetType())) if (input.HasFlag(value)) yield return value; } 回答2: Here is a Linq solution to the problem. public static IEnumerable<Enum> GetFlags(this Enum e) { return Enum.GetValues(e