enums

Anyone know a good workaround for the lack of an enum generic constraint?

What I want to do is something like this: I have enums with combined flagged values. public static class EnumExtension { public static bool IsSet<T>( this T input, T matchTo ) where T:enum //the constraint I want that doesn't exist in C#3 { return (input & matchTo) != 0; } } So then I could do: MyEnum te...

Enum Flags Attribute

Anyone have a good explanation or example they could post? Edit: I changed the answer, this one is more in depth. ...

How do I Convert a string to an enum in C#?

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to the enumeration value. In an ideal world, I could do something like this: StatusEnum M...

Anyone know a quick way to get to custom attributes on an enum value?

This is probably best shown with an example. I have an enum with attributes: public enum MyEnum { [CustomInfo("This is a custom attrib")] None = 0, [CustomInfo("This is another attrib")] ValueA, [CustomInfo("This has an extra flag", AllowSomething = true)] ValueB, } I want to get to those attributes from an ...

Cast int to Enum in C#

What's a quick and easy way to cast an int to an enum in c#? ...

add preddefined data for typedef enums in c

What is the best approach to define additional data for typedef enums in C? Example: typedef enum { kVizsla = 0, kTerrier = 3, kYellowLab = 10 } DogType; Now I would like to define names for each, for example kVizsla should be "vizsla". I currently use a function that returns a srting using a large switch block. ...

What's the best way to implement an 'enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python. What's the best way to implement the equivalent of an enum in Python? ...

Using attributes to cut down on enum to enum mapping and enum/const to action switch statments

I imagine everyone has seen code like: public void Server2ClientEnumConvert( ServerEnum server) { switch(server) { case ServerEnum.One: return ClientEnum.ABC //And so on. Instead of this badness we could do somthing like: public enum ServerEnum { [Enum2Enum(ClientEnum.ABC)] One, } Now we c...

How to Compare Flags in C#?

I have a flag enum below. [Flags] public enum FlagTest { None = 0x0, Flag1 = 0x1, Flag2 = 0x2, Flag3 = 0x4 } I cannot make the if statement evaluate to true. FlagTest testItem = FlagTest.Flag1 | FlagTest.Flag2; if (testItem == FlagTest.Flag1) { // Do something, // however This is never true. } How can I mak...

Querying collections of value type in the Criteria API in Hibernate

Hi, In my database, I have an entity table (let's call it Entity). Each entity can have a number of entity types, and the set of entity types is static. Therefore, there is a connecting table that contains rows of the entity id and the name of the entity type. In my code, EntityType is an enum, and Entity is a Hibernate-mapped class. in ...

Combining Enums

Is there a way to combine Enums in VB.net? ...

UserControl Property of type Enum displays in designer as bool or not at all

I have a usercontrol that has several public properties. These properties automatically show up in the properties window of the VS2005 designer under the "Misc" category. Except two of the properties which are enumerations don't show up correctly. The first on uses the following enum: public enum VerticalControlAlign { Center, ...

Best way to represent a parameterized enum in C#?

Are there any good solutions to represent a parameterized enum in C# 3.0? I am looking for something like OCaml or haXe has. I can only think of class hierarchy with a simple enum field for easy switching for now, maybe there are better ideas? See Ocaml example below in one of the replies, a haXe code follows: enum Tree { Node(left:...

Forward declaring an enum in c++

Hi guys, I'm trying to do something like the following: enum E; void Foo(E e); enum E {A, B, C}; which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why. Can anyone explain? Many thanks. Clarification 2: I'm doing this as I have private methods in a...

Map of Enums and dependency injection in Spring 2.5

Let's assume we've got the following Java code: public class Maintainer { private Map<Enum, List<Listener>> map; public Maintainer() { this.map = new java.util.ConcurrentHashMap<Enum, List<Listener>>(); } public void addListener( Listener listener, Enum eventType ) { List<Listener> listeners; if( ( listen...

Enums in Ruby

What's the best way to implement the enum idiom in Ruby? I'm looking for something which I can use (almost) like the Java/C# enums. ...

64 bit enum in C++?

Is there a way to have a 64 bit enum in C++? Whilst refactoring some code I came across bunch of #defines which would be better as an enum, but being greater than 32 bit causes the compiler to error. For some reason I thought the following might work: enum MY_ENUM : unsigned __int64 { LARGE_VALUE = 0x1000000000000000, }; ...

eliminating duplicate Enum code

Hi, I have a large number of Enums that implement this interface: /** * Interface for an enumeration, each element of which can be uniquely identified by it's code */ public interface CodableEnum { /** * Get the element with a particular code * @param code * @return */ public CodableEnum getByCode(String ...

Mapping a collection of enums with NHibernate

Mapping a collection of enums with NHibernate Specifically, using Attributes for the mappings. Currently I have this working mapping the collection as type Int32 and NH seems to take care of it, but it's not exactly ideal. The error I receive is "Unable to determine type" when trying to map the collection as of the type of the enum I ...

Create Generic method constraining T to an Enum

I'm building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; fore...