enums

Would you use regions within long switch/enum declarations?

I've recently found myself needing (yes, needing) to define absurdly long switch statements and enum declarations in C# code, but I'm wondering what people feel is the best way to split them into logical subsections. In my situation, both the enum values and the cases (which are based on the enum values) have fairly clear groupings, yet ...

Switch Statement for Enum Value Representations in Java

I'm very familiar with using Enums in other languages, but I'm having some difficulty in Java with a particular use. The Sun documentation for Enums boldly states: "Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers." Well, that's dandy, ...

How to improve this enumeration type ?

I will often need to translate from the string code into its Enum type. See snippet below. I think this is highly inefficient. How can I improve the method deref so that: It will be threadsafe. It is faster. Code: public enum SigninErrorCodes { InvalidUser("a0"), InvalidPassword("b5"), NoServerResponse("s2"); SigninErrorCod...

What to do when bit mask (flags) enum gets too large.

I have a very large set of permissions in my application that I represent with a Flags enumeration. It is quickly approaching the practical upper bound of the long data type. And I am forced to come up with a strategy to transition to a different structure soon. Now, I could break this list down into smaller pieces, however, this is a...

C# explicit cast string to enum

Hi, I would like to have an explicit cast between from a string to an enum in c# in order to have this : (MyEnum) Enum.Parse(typeof(MyEnum),stringValue) I would like to deport this into an explicit cast operator, I did this but didn't work : public static explicit operator (MyEnum)(value stringValue){ return (MyEnum) Enum.Pars...

can set enum start value in java

Hi, I use the enum make a few constants: enum ids {OPEN, CLOSE}; the OPEN'valuse is zero, but I wanna it as 100. Is it possible? ...

How can I require a generic parameter to be an enum that implements an interface?

I'm not 100% convinced that this is a good idea, but I bumped into some code today that's currently implemented as: class MyWidget <T extends Enum<T> > { MyWidget(Map<T, Integer> valueMap) { mValueMap = valueMap; } Map<T, Integer> mValueMap; } where MyWidget then offers methods that use mValueMap to convert the passed-in En...

Overload Resolution with Two Enums

We have some code that looks roughly like this: // Two enums that differ entirely. enum A { a1, a2 }; enum B { b1, b2 }; // Functions to convert in some meaningful way between them A convert(B); B convert(A); Now, our compiler goes and does exactly what we expect it to. convert(a1) will call B convert(A), and so on. However, when w...

How do I avoid compiler warnings when converting enum values to integer ones?

I created a class CMyClass whose CTor takes a UCHAR as argument. That argument can have the values of various enums (all guaranteed to fit into a UCHAR). I need to convert these values to UCHAR because of a library function demanding its parameter as that type. I have to create a lot of those message objects and to save typing effort I ...

How to test enum types?

Hey there, i'm currently trying to build a more or less complete set of unit tests for a small library. Since we want to allow different implementations to exist we want this set of tests to be a) generic, so that we can re-use it to test the different implementations and b) as complete as possible. For the b) part i'd like to know if t...

how can I lookup a Java enum from its string value?

I would like to lookup an enum from its string value (or possibly any other value). I've tried the following code but it doesn't allow static in initialisers. Is there a simple way? public enum Verbosity { BRIEF, NORMAL, FULL ; private static Map<String, Verbosity> stringMap = new HashMap<String, Verbosity>(); private Verbosity() ...

How to TryParse for Enum value?

I want to write a utility wherein can validate a given value (string) against possible values of enum. And in case of match, return the enum instance, else return a default value. PS: I don't want to user Try...catch here. public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue){ TEnum enumValue; if(!TryPars...

How do I find an enum value given the arguments that were supplied to its constructor?

I have an enum class like this: public enum Position { A1(0,0), A2(1,0), //etc public final int dy, dx; private Position(int dy, int dx) { this.dy = dy; this.dx = dx; } } Now I want a method: public static Position getPosition(int dx, int dy) Can I return Position.A1 or Position.A2 with the gi...

Comparing enum flags in C#

Hello folk, I need to detect if a flag is set within an enum value, which type is marked with the Flag attribute. Usually it is made like that: (value & flag) == flag But since I need to do this by generic (sometimes at runtime I event have only an "Enum" reference. I can not find an easy way to use the & operator. At the moment I m...

Scala - Java interop: can Scala emit enums in bytecode for Java to consume?

I have a project that is mixed Java/Scala, it is Java GUI code that makes use of a Scala library. Is there a way to write Scala code such that it will emit Java enums on compile time? The approaches I tried so far (sealed case classes, extend Enumeration) seem to generate normal classes which makes working with them from Java much hairie...

Convert objective-c typedef to its string equivalent

Assuming that I have a typedef declared in my .h file as such: typedef enum { JSON, XML, Atom, RSS } FormatType; I would like to build a function that converts the numeric value of the typedef to a string. For example, if the message [self toString:JSON] was sent; it would return 'JSON'. The function would look something lik...

How to get the count of enumerations?

How to get the count of total values defined in an enumeration? ...

Enumerate with return type other than string?

Since enumeration uses integers, what other structure can I use to give me enum-like access to the value linked to the name: [I know this is wrong, looking for alternative] private enum Project { Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"), Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"), ...

Switch statement without default when dealing with enumerations

This has been a pet peeve of mine since I started using .NET but I was curious in case I was missing something. My code snippet won't compile (please forgive the forced nature of the sample) because (according to the compiler) a return statement is missing: public enum Decision { Yes, No} public class Test { public stri...

C enums question

I'm not sure what is the proper syntax for using C enums. I have the following code: enum {RANDOM, IMMEDIATE, SEARCH} strategy; strategy = IMMEDIATE; But this does not compile, with the following error: error: conflicting types for ‘strategy’ error: previous declaration of ‘strategy’ was here What am I doing wrong? ...