enums

How to use enums with bit flags

I have an enum declaration using bit flags and I cant exactly figure out on how to use this. enum { kWhite = 0, kBlue = 1 << 0, kRed = 1 << 1, kYellow = 1 << 2, kBrown = 1 << 3, }; typedef char ColorType; I suppose to store multiple colors in one colorType I should OR the bits together? ColorType pinkColor = kW...

Defining Status of data via Enum or a relation table

I have an application which has rows of data in a relation database the table needs a status which will always be either Not Submitted, Awaiting Approval, Approved, Rejected Now since these will never change I was trying to decide the best way to implement them I can either think of a Status enum with the values and an int assigned whe...

Serialization problem with Enums at Android

I'm using XStream to serialize some objects to XML, and am facing a problem with Enums. The exception I get when I try to serialize the object: "ObjectAccessException: invalid final field java.lang.Enum.name". Apparently, this is a problem with the reflection API implementation in android: It doesn't treat final fields correctly. This p...

How to specify the integral type for my enum type like in C#?

In C# I can write something like this: enum MyEnum : byte { Value1, Value2, Value3 } and the integral type of MyEnum will be byte. In Objective-C I want the size of MyEnum to be 1 byte. How can I do it? ...

How to throw an exception from an enum constructor?

How can I throw an exception from an enum constructor? eg: public enum RLoader { INSTANCE; private RLoader() throws IOException { .... } } produces the error Unhandled exception type IOException ...

TINYINT vs ENUM(0, 1) for boolean values in MySQL

Which one is better, Tinyint with 0 and 1 values or ENUM 0,1 in MyISAM tables and MySQL 5.1? ...

BIT(1) vs ENUM('unknown', 'male', 'female') in MySQL

In performance terms, what will be faster, use a BIT(1) NULL (null = unknown, 0 = male, 1 = female) or ENUM('unknown', 'male', 'female') NOT NULL DEFAULT 'unknown' in MySQL MyISAM? Or this is considered micro-optimization? [EDIT] I think I'm going to use ENUM('male', 'female') DEFAULT NULL ...

How to use a flags enum as a property of a custom component in SSIS?

Hi, I'm programming a custom component for SSIS in which I need the following Enum as a property I can edit (selection of multiple values is needed). [Flags] public enum PermissionSettings : ushort { None = 0, Groups = 1, ADGroups = 2, Users = 4, Owner = 8, OwnerGroup = 16, PublicAccess = 32, System = 6...

How can I get the Agent Smith ReSharper plugin to recogize that Status is not plural

When using the Agent Smith plugin for ReSharper, I get the warning "Enums that are not flags should not have plural names" for an enum that ends in Status. For example: public enum SomeStatus { Success, Failed } In fact Status is not plural, so this isn't really in violation of the naming rule. I found this ticket from 2008 br...

How to override the (final) equals method in java enums?

Hi, I have a problem with overriding the equals method in an Enum to make it compatible with other classes. The Enum implements an interface and the idea is that all implementations of this interface can be tested for equality, regardless of their type. For Example: public interface Group { public Point[] getCoordinates(); } publ...

Getting Enum value via reflection

I have a simple Enum public enum TestEnum { TestOne = 3, TestTwo = 4 } var testing = TestEnum.TestOne; And I want to retrieve its value (3) via reflection. Any ideas on how to do this? Thanks Mat ...

Use EnumType.None, or Nullable<EnumType>?

Enums are generally used to define the state of a particular property of a class, say in an object model of some sort. For some of these properties, the state 'this property is not set' is valid. In these situations, should I use a zero None enum value, or make the property type nullable? public MyEnum Property { get; set; } public en...

Using enums in WCF Data Services

I'm trying to manually build a WCF Data Service using a POCO data model and I cannot figure out how to properly expose enum values. Assuming a simple model like: public class Order { public int ID {get; set;} public string Description {get; set;} public OrderStatus Status {get; set;} } public enum OrderStatus { New, InPr...

Java: Having trouble declaring an enum with integer constants

Urgh, I'm kind of confused on how enums work in Java. In C# and C++ (what I use normally), this seems okay, but Java wants to get mad at me >.> enum Direction { NORTH_WEST = 0x0C, NORTH = 0x10, NORTH_EAST = 0x14, WEST = 0x18, NONE = 0x20, EAST = 0x28, SOUTH_WEST = 0x...

Why does Enum.Parse create undefined entries?

class Program { static void Main(string[] args) { string value = "12345"; Type enumType = typeof(Fruits); Fruits fruit = Fruits.Apple; try { fruit = (Fruits) Enum.Parse(enumType, value); } catch (ArgumentException) { Console.WriteLine(String.F...

Java enum best practice

This might seem like a trivial question, but I'm a bit muddled in my thinking regarding enums.. So I have a class - let's say its called DVDPlayer - and I want to have an enum that represents whether it's ON, OFF, or STANDBY. So I can put the enum in the class - it doesn't make sense outside of the class. My question is this - should ...

Enums scope declaration error

I have namespace with enums: namespace Search { enum SearchConditionType { Like = 0, EqualNotString = 1, EqualString = 2 }; } Then I try to declare enum: namespace Search { public partial class Controls_SelectYesNo : System.Web.UI.UserControl { public SearchConditionType Field; ...

Is the Visitor pattern the best way to refactor domain enums to classes?

If we want to refactor an enum (contained in the domain layer) to a polymorphic class, using "simple" abstract methods could be a bad idea, if all the switch and if statements we want to refactor are inside the other layers (like the business or the presentation layer), because we could end up to reference these layers inside the domain ...

Enum value as hidden in C#

Hi, I have an enum and i want to "hide" one of its values (as i add it for future support). The code is written in C#. public enum MyEnum { ValueA = 0, ValueB = 1, Reserved } I don't want to allow peoples who use this code to use this values (MyEnum.Reserved). Any idea? TIA ...

Generic method, unboxing nullable enum

I've made the following extension method ... public static class ObjectExtensions { public static T As<T>(this object pObject, T pDefaultValue) { if (pObject == null || pObject == DBNull.Value) return pDefaultValue; return (T) pObject; } } ... which i use for e.g. reading data like so: string f...