enum-flags

Proper design pattern for passing flags to an object

半城伤御伤魂 提交于 2019-12-11 13:35:34
问题 In C I've done this sort of thing enum { USE_COKE = 1, USE_PEPSI = 2, USE_JUICE = 4, USE_WATER = 8 }; int makeDrink(int flags); //... int rcode = makeDrink(USE_COKE | USE_JUICE | USE_WATER); I know this is pretty standard, it's also used in iostream for instance. I'm wondering how to translate this design pattern into Java or OOP? I'm pretty sure polymorphism is not the way to go here since it'd be better for my code to have an if(flag_is_set) block than rewrite much of the routine. Is there

Access Control in ASP.NET MVC using [Flags] based Enum to int permissions management in SQL

痴心易碎 提交于 2019-12-10 12:20:21
问题 This question is inspired by this SO question regarding Access Control in ASP.NET MVC. Here I'm trying to bring the accepted answer into a tangible solution. The answer mentions using FileSystemSecurity as an inspiration to managing permissions. Here I'm also using an enum with Flags attribute to define the ACL for all my objects. Additionally each member of my objects will be stored in a column within SQL. Assume a simplified Linq2SQL, EF, or nHibernate ORM mapping. Edit: Added the following

C# Enums - Check Flags against a Mask

妖精的绣舞 提交于 2019-12-10 04:10:17
问题 I have the following enum flags: [Flags] private enum MemoryProtection: uint { None = 0x000, NoAccess = 0x001, ReadOnly = 0x002, ReadWrite = 0x004, WriteCopy = 0x008, Execute = 0x010, ExecuteRead = 0x020, ExecuteReadWrite = 0x040, ExecuteWriteCopy = 0x080, Guard = 0x100, NoCache = 0x200, WriteCombine = 0x400, Readable = (ReadOnly | ReadWrite | ExecuteRead | ExecuteReadWrite), Writable = (ReadWrite | WriteCopy | ExecuteReadWrite | ExecuteWriteCopy) } Now i have an enum instance that I need to

HasFlag with a generic enum?

≯℡__Kan透↙ 提交于 2019-12-08 18:46:44
问题 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<TEnum> where TEnum : struct { } How can I add the [Flags] attribute to it? 回答1: Calling the instance method will require boxing anyway, so, since you can't constrain to Enum , just abandon generics and use Enum . For example, instead of: void Something(TEnum enumValue, TEnum flags) { if (enumValue.HasFlags(flags)) //do something ... } Do this: void

Convert some bool properties to a flags enum

陌路散爱 提交于 2019-12-05 07:13:11
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.PropC) { throw new ArgumentException(); } if (legacyClass.PropA) { FlagEnum = FlagEnum.EnumValue1; }

C# Enums - Check Flags against a Mask

你。 提交于 2019-12-05 04:18:50
I have the following enum flags: [Flags] private enum MemoryProtection: uint { None = 0x000, NoAccess = 0x001, ReadOnly = 0x002, ReadWrite = 0x004, WriteCopy = 0x008, Execute = 0x010, ExecuteRead = 0x020, ExecuteReadWrite = 0x040, ExecuteWriteCopy = 0x080, Guard = 0x100, NoCache = 0x200, WriteCombine = 0x400, Readable = (ReadOnly | ReadWrite | ExecuteRead | ExecuteReadWrite), Writable = (ReadWrite | WriteCopy | ExecuteReadWrite | ExecuteWriteCopy) } Now i have an enum instance that I need to check if it's readable. If I use the following code: myMemoryProtection.HasFlag(MemoryProtection

C#: Getting the correct keys pressed from KeyEventArgs' KeyData

半世苍凉 提交于 2019-12-04 08:44:48
问题 I am trapping a KeyDown event and I need to be able to check whether the current keys pressed down are : Ctrl + Shift + M ? I know I need to use the e.KeyData from the KeyEventArgs , the Keys enum and something with Enum Flags and bits but I'm not sure on how to check for the combination. 回答1: You need to use the Modifiers property of the KeyEventArgs class. Something like: //asumming e is of type KeyEventArgs (such as it is // on a KeyDown event handler // .. bool ctrlShiftM; //will be true

Using checkboxes to PostBack Enum with Flags

萝らか妹 提交于 2019-12-03 13:02:04
问题 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 value and gave every checkbox the same name. During postback the first selected flag is retrieved but not concatenated with the other flags. Could I fix this somehow without having separate property for each flag? Model public class HomeModel {

Using checkboxes to PostBack Enum with Flags

有些话、适合烂在心里 提交于 2019-12-03 03:15:39
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 value and gave every checkbox the same name. During postback the first selected flag is retrieved but not concatenated with the other flags. Could I fix this somehow without having separate property for each flag? Model public class HomeModel { public Fruit MyFruits { get; set; } } [Flags] public enum Fruit { Love = 1, Joy = 2, Peace = 4, Patience = 8

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

喜夏-厌秋 提交于 2019-12-01 02:44:00
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.HasFlag(Animals.Zebra); //false HasFlag is effectively this: HasFlag = (GivenFlag & Value) ==