enums

Using Enum values as integers

I have the following enumerated type: /// <summary> /// TTE Node types. /// </summary> public enum E_TTE_NODES { /// <summary> /// Represents FCM 0 /// </summary> E_FCM0 = 0, /// <summary> /// Represents FCM 1 ///...

Why does [Flag]'d enums start at 0 and increment by 1?

Edit: It seems most people misunderstood my question. I know how enum works, and I know binary. I'm wondering why the enums with the [Flags] attribute is designed the way it is. Original post: This might be a duplicate, but I didn't find any other posts, so here goes. I bet there has been some good rationale behind it, I just find it...

Polymorphic Enum in C++

I have these Enum declarations: enum MessageType{ REQ_LOGIN, REQ_GET_FIELD, RES_LOGIN, RES_GET_FIELD } enum Request{ REQ_LOGIN, REQ_GET_FIELD }; enum Respond{ RES_LOGIN, RES_GET_FIELD }; Obviously I'm repeating elements in Enum's. Is there any way to prevent this? EDIT: I'm using "MessageTy...

Is it possible to use custom enums in application settings? (VS10)

In Visual Studio 10 (probably other versions too) it's possible to define application settings using a designer view. These settings appear to simply be public variables stored. Is there any way to use a custom enum as the type of an application setting? I notice that you can browse the references for enums, but can't seem to find a wa...

Generics doesn't work for return type

How come java doesn't allow the following generic return type: public <T extends Enum<T> & MyInterface> Class<T> getEnum() { return MyEnum.class; } While the following does work: public <T extends Enum<T> & MyInterface> Class<T> getEnum(Class<T> t) { return t; } getEnum(MyEnum.class); MyEnum is an enumaration that impleme...

How do I select a random value from an enumeration?

Given an arbitrary enumeration in C#, how do I select a random value? (I did not find this very basic question on SO. I'll post my answer in a minute as reference for anyone, but please feel free to post your own answer.) ...

C# in VS2005: can you check whether an integer is delcared in a given Enum type?

For C# in VS2005, is there way to check if an integer is part of a Enum type? eg: if number in CustomerType { ... } where enum CustomerType { A = 0; B = 1; C = 2; } ...

C# Regex to match enum body type

I have this enum defitiion in file: public enum IdentityState { [EnumMember] New = 0, [EnumMember] Normal = 1, [EnumMember] Disabled = 2 } { some other data... } And want to match only body of this enum (between {}), the match result i want is: { [EnumMember] New = 0, [EnumMember] Normal = 1, ...

Is using enum for integer bit oriented operations in C++ realiable/safe?

Consider the following (simplified) code: enum eTestMode { TM_BASIC = 1, // 1 << 0 TM_ADV_1 = 1 << 1, TM_ADV_2 = 1 << 2 }; ... int m_iTestMode; // a "bit field" bool isSet( eTestMode tsm ) { return ( (m_iTestMode & tsm) == tsm ); } void setTestMode( eTestMode tsm ) { m_iTestMode |= tsm...

Why is it okay for an enum to have two different names with the same numeric value?

I just discovered a subtle bug where I had an enum with two names unintentially sharing the same numeric value (in this case red=10 and crimson=10). I'm a bit surprised this isn't a syntax error. public enum Colour { Red=10, Blue=11, Green=12, Crimson=10 } // Debug.Write(Colour.Red==Colour.Crimson) outputs True Is ther...

How to create a file of constants in Javascript?

Hi, Is there a way to create a file of constants in JavaScript, which I can reference and then use? What I am looking for is something like this: Constants.js: var Phones = { Nokia: 1, Samsung: 2 } Then, in another JavaScript file JS2.js access those values: JS2.js: alert(Phones.Nokia); And then, in ...

C#: Enums in Interfaces

Hey all, I've seen a couple similar threads to this question, but none of them really answer the question I want to ask. For starters, unfortunately I'm working with existing API code so sadly, while there may be a better way to do what I'm asking about, I'm locked in to doing it similarly to the way it is because backwards compatibili...

Strange (possibly wrong?) C# compiler behavior with method overloading and enums

Hi, today I discovered a very strange behavior with C# function overloading. The problem occurs when I have a method with 2 overloads, one accepting Object and the other accepting Enum of any type. When I pass 0 as parameter, the Enum version of the method is called. When I use any other integer value, the Object version is called. I kno...

How can I test if an enum is defined or not whilst ignoring case?

The following generic static method takes a string and returns an enum. It nicely ignores case since I set the ignoreCase parameter to true. However, I also want to test if the enum exists, but the enum.IsDefined method to do this doesn't seem to have an ignoreCase parameter. How can I test if the enum is defined or not and at the sa...

How to create an array of enums

I have about 30 different flagged enums that I would like to put into an array for indexing and quick access. Let me also claify that I do not have 1 enum with 30 values but I have 30 enums with differing amounts of values. The goal would be to add them to an array at a specifed index. This way I can write a functions in which I can pas...

C#: Should the default value of an enum be None or Unknown?

Hey all, Say you have an enum that represents an error code. There will be several codes, each with their own underlying int value; however, the enum value that gets the default 0 value seems like it should be carefully considered. In the case of an error code enum, there are two special values that I can think of: None (for cases when...

Print text instead of value from C enum

int main() { enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}; Days TheDay; int j = 0; printf("Please enter the day of the week (0 to 6)\n"); scanf("%d",&j); TheDay = Days(j); //how to PRINT THE VALUES stored in TheDay printf("%s",TheDay); // isnt working return 0; } ...

How would one go about dealing with generic enums in Java?

So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>> {...} Somewhere in a non-abstract method inside AbstractParent, I would like to iterate over the values of E. Is this possible? For a better example: public abstract class AbstractParent <E extends Enum<E>> { ... protected void doSome...

Make an enum INSTANCE private

I am using an enum singletom pattern like this: public enum LicenseLoader implements ClientLicense { INSTANCE; /** * @return an instance of ClientLicense */ public static ClientLicense getInstance() { return (ClientLicense)INSTANCE; } ...rest of code } Now I want to return the Interface and hide...

Why does the number of elements in an enumeration change the visibility of a private typedef in C++?

Sorry for the not very descriptive question title. I'm not very sure how to describe this, hopefully I can make it better later if someone can explain this to me. I was about to come on here and ask why the following example was working. It was exhibiting the behaviour I was hoping for but I wasn't sure why or whether it was standard or...