enums

How to prevent duplicate values in enum?

Hi, I wonder is there a way to prevent an enum with duplicate keys to compile? For instance this enum below will compile public enum EDuplicates { Unique, Duplicate = 0, Keys = 1, Compilation = 1 } Although this code Console.WriteLine(EDuplicates.Unique); Console.WriteLine(EDuplicates.Duplicate); Console.WriteLine(E...

Enum index by Value

[FlagsAttribute] public enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; I have an enum as show. I want the ability to get say Colors.Blue is at index 2, index staring from 0.I want to get the index number passing in Colors.Whatever? Can someone post me some snippets... ...

How to use flags enums in Linq to Entities queries?

Hi, I have a [Flags] enum like this: [Flags] public enum Status { None = 0, Active = 1, Inactive = 2, Unknown = 4 } A Status enum may contain two values such as: Status s = Status.Active | Status.Unknown; Now I need to create a linq query (LINQ to ADO.NET Entities) and ask for records whose status is s above, that is Acti...

Is it acceptable to design a method with a parameter of type System.Enum?

Consider the following method, public void Add(Enum key, object value); Since Enum is a "special class", I didn't realize you could use the type in this way, but it compiles. Now, the .NET Framework Design Guidelines don't have anything to say about this construction, so I'm curious what everyone else thinks about this. Here is what...

Using an Enum as an Attribute Argument

Here is the code I would like to use: public enum Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri }; [EnumHelper(typeof(Days))] public Days DayOfWeek { get; set; } EnumHelper looks like: [AttributeUsage(AttributeTargets.Property,AllowMultiple=true)] public class EnumHelper : Attribute { public Type MyEnum { get; set; } public E...

Instance validation error: '2' is not a valid value for QueryType. (web service)

I have a web service that I am passing an enum public enum QueryType { Inquiry = 1 Maintainence = 2 } When I pass an object that has a Parameter of QueryType on it, I get the error back from the web service saying that '2' is not a valid value for QueryType, when you can clearly see from the declaration of the enum that it is....

Can Java methods return type Enum?

I could be wrong but I'm guessing from this previous SO post that, since an enum in Java cannot be declared locally, that therefore it is therefore problematic for a method to return type Enum? I can declare that a method should return an Enum (see below) but how would one then go about implementing such a method to return anything othe...

Enum and their Values

What would be the value of Field.Format("%04d", ErrorCode) in the procedure below if the AErrorCode is ERR_NO_HEADER_RECORD_FOUND_ON_FILE? Somewhere in a .h file: enum AErrorCode { ERR_UNKNOWN_RECORD_TYPE_CODE = 5001, ERR_NO_HEADER_RECORD_FOUND_ON_FILE, ERR_DUPLICATE_HEADER_RECORD_FOUND, ERR_THIRD_PARTY_LETTER_RECORD_H...

short enums on VC++ 6.0 (or VC++ 2008) ?

It would be very convenient to have "short enum" or "char enum" in VC++, as I have seen mentioned elsewhere for GCC etc. Is there some option in VC++ to allow this? ...

How to use enums as flags in C++?

Treating enums as flags works nicely in C# via the [Flags] attribute, but what's the best way to do this in C++? For example, I'd like to write: enum AnimalFlags { HasClaws = 1, CanFly =2, EatsFish = 4, Endangered = 8 }; seahawk.flags = CanFly | EatsFish | Endangered; However, I get compiler errors regarding int/enum...

Declaring Enums across derived classes

Hi, I am developing a program in VB.NET. I have an enum called PriceType that is declared in a super class ItemPriceBase. There are 3 classes that derive from ItemPriceBase - ItemPriceMeasured, ItemPriceNDI and ItemPriceExtraCost. The subset of PriceTypes for these classes are totally unique from each - Measured prices are 1 to 6, NDI...

visual c++ enum (CloseReason)

When I write the following: private: System::Void queue_FormClosing( System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) { if(e->CloseReason!=CloseReason::FormOwnerClosing) e->Cancel=true; } I get this error: ###\queue.h(153) : error C2039: 'FormOwnerClosing' : is not a member of 'System::Windows:...

how to cast to CRTP in java ?

Hi all, I have a pretty simple case where I do some basic generic assignment: final Detail detail = field.getAnnotation(Detail.class); final String example = detail.example(); final Class<?> type = field.getType(); if (List.class.isAssignableFrom(type)) ... else if (Enum.class.isAssignableFrom(type)) setValue(c...

Populating Swing JComboBox from Enum

I would like to populate a java.swing JComboBox with values from an Enum. e.g. public enum Mood { HAPPY, SAD, AWESOME; } and have these three values populate a readonly JComboBox. Thanks! ...

Advanced Java-like enums in Ruby

First of all, this is not a duplicate of Enums in Ruby :) The accepted answer of that question suggests this as a good way to represent enums in Ruby: class Foo BAR = 1 BAZ = 2 BIZ = 4 end In Java it is possible to attach multiple values and methods to an enum value. I want to achive the same or something similar in Ruby. What...

Creating ENUM variable type in MySQL

I am using an ENUM data type in MySQL and would like to reuse it, but not retype in the values. Is there an equivalent to the C, C++ way of defining types in MySQL? I would like to do the following: DEFINE ETYPE ENUM('a','b','c','d'); CREATE TABLE Table_1 (item1 ETYPE, item2 ETYPE); Is this possible? Thanks ...

What's the simplest way to compare an emum to a integer value return from a DB

I'm pulling some data from a table using LINQ 2 SQL...one of the pieces of data is a value that represents an enumation in my application code. What is the simplest way to make a comparison between the data returned in LINQ objects and the enumeration in the application code. So for example enum SomeEnum { First Second } then in...

Getting total number of enum items

Is it possible to get the total number of items defined by an enum at runtime? While it's pretty much the same question as this one, that question relates to C#, and as far as I can tell, the method provided there won't work in Objective-C. ...

C#:Map Enum to [Flags] Enum

I have an Enum, suppose: public enum ItemStatus { Available, Unavailable } I have a method that returns a list of those TVs, based on a filter. And a filter is represented by an Enum: [Flags] public enum ItemStatusFilter { Available = 1, Unavailable = 2 } Question: what is a slick way to check if given instance of ItemStatu...

Can you access a long description for a specific enum value.

I usually access enum description for a corresponding value like: Enum.GetName(typeof(MyEnum), myid); I need to have an enum that could use any chars like "hello world %^$£%&" I've seen people attaching an attribute and adding extensions like here: http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.as...