enum-flags

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

和自甴很熟 提交于 2019-11-30 21:36:16
问题 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 = Animals.Dog | Animals.Horse; var hasNone = myAnimals.HasFlag(Animals.None); //true! Why? var hasCat = myAnimals.HasFlag(Animals.Cat); //false var hasDog = myAnimals.HasFlag(Animals.Dog); //true var hasHorse = myAnimals.HasFlag(Animals.Horse); //true var hasZebra = myAnimals

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

◇◆丶佛笑我妖孽 提交于 2019-11-30 11:38:51
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)) { // Doesn't work } Given the documentation , I'd expect that to return true if the value has both of

Setting multiple enum flags in XAML

放肆的年华 提交于 2019-11-30 00:59:06
问题 Is there any way to set multiple enum flags (that are traditionally separated by | in codebehind) in XAML? I tried something like: <ns:SomeControl Flags="FlagA|FlagB" /> but that didn't work. 回答1: WPF does support this through a type converter. It can be done by using a comma in between enum values: <ns:SomeControl Flags="FlagA,FlagB" /> 来源: https://stackoverflow.com/questions/7718928/setting-multiple-enum-flags-in-xaml

Model Bind List of Enum Flags

感情迁移 提交于 2019-11-29 19:33:42
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 enum NotificationDeliveryType { InSystem = 1, Email = 2, Text = 4 } I found this article but he's getting back a single flag value and he's binding it in the controller like this (with a days of the week concept): [HttpPost] public ActionResult MyPostedPage(MyModel model) { //I moved the logic for setting this into a helper //because this could be re

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

旧时模样 提交于 2019-11-29 07:08:22
问题 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>(T value,T lookingForFlag) where T:enum { Boolean result = ((value & lookingForFlag) == lookingForFlag); return result ; } 回答1: No, you can't do this with C# generics. However, you could do: public static bool IsEnumFlagPresent<T>(T value, T lookingForFlag) where T : struct

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

坚强是说给别人听的谎言 提交于 2019-11-28 15:50:52
问题 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 a cleaner way of doing this, and it should be easier to understand for other developers. [Flags()] public enum Weekdays : int { Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8, Friday = 16, Saturday = 32, Sunday = 64 } However, as I started to

Model Bind List of Enum Flags

拜拜、爱过 提交于 2019-11-28 15:10:09
问题 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 enum NotificationDeliveryType { InSystem = 1, Email = 2, Text = 4 } I found this article but he's getting back a single flag value and he's binding it in the controller like this (with a days of the week concept): [HttpPost] public ActionResult

Why are flag enums usually defined with hexadecimal values

与世无争的帅哥 提交于 2019-11-27 16:55:03
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 usually declare it like this: [Flags] public enum MyEnum { None = 0, Flag1 = 1, Flag2 = 2, Flag3 = 4, Flag4 = 8, Flag5 = 16 } Is there a reason or rationale to why some people choose to write the value in hexadecimal rather than decimal? The way I see it, it's easier to get confused when using hex values and accidentally write Flag5 = 0x16 instead of Flag5 = 0x10 . Rationales

How to reduce code duplication in ASP.NET MVC view when working with Flags enum

元气小坏坏 提交于 2019-11-27 08:14:34
问题 Forgive my ignorance. Not done a lot of MVC work, and I'm sure there must be a better way to do this but I can't seem to find it. I have a Flags enum like this: [Flags] public enum Services { Foo = 1, Bar = 2, Meh = 4 } And a SelectedServices property on my Model which has a value of this type. In the View, I have a checkbox for each possible service. I have implemented the binding logic like so: <div><label><input type="checkbox" name="services" value="@((int)Services.Foo)" @if(Model

Why are flag enums usually defined with hexadecimal values

烈酒焚心 提交于 2019-11-26 18:47:23
问题 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 usually declare it like this: [Flags] public enum MyEnum { None = 0, Flag1 = 1, Flag2 = 2, Flag3 = 4, Flag4 = 8, Flag5 = 16 } Is there a reason or rationale to why some people choose to write the value in hexadecimal rather than decimal? The way I see it, it's easier to get