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...
Anyone have a good explanation or example they could post?
Edit: I changed the answer, this one is more in depth.
...
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...
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 ...
What's a quick and easy way to cast an int to an enum 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.
...
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?
...
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...
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...
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 ...
Is there a way to combine Enums in VB.net?
...
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,
...
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:...
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...
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...
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.
...
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,
};
...
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
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 ...
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...