enums

Why can I parse invalid values to an Enum in .NET?

Why is this even possible? Is it a bug? using System; public class InvalidEnumParse { public enum Number { One, Two, Three, Four } public static void Main() { string input = "761"; Number number = (Number)Enum.Parse(typeof(Number), input); Console.WriteLine(nu...

In C#, how to easily map enum flags from one type to another?

Also see the updates at the end of the question... Given the following situation: [Flags] enum SourceEnum { SNone = 0x00, SA = 0x01, SB = 0x02, SC = 0x04, SD = 0x08, SAB = SA | SB, SALL = -1, } [Flags] enum DestEnum { DNone = 0x00, DA = 0x01, DB = 0x02, DC = 0x04, DALL = 0xFF, } I...

Bitfield enum extension method to return dictionary of included values

Enums can sure be confusing. I am trying to create an extension method on the Enum type that will take a value and return the names of all the bits that match. Given: [Flags] public enum PlanetsEnum { Mercury=1, Venus=2, Earth=4, Mars=8, Jupiter=16, //etc.... } I would like to create an extension method that return a dic...

Fluent NHibernate - How to use an enumerator to identify subclass?

Hi, Im trying to map the following classes: public abstract class ScheduleType { public virtual int Id { get; set; } public virtual TypeDiscriminatorEnum Discriminator { get; set; } } public class DerivedScheduleType : ScehduleType { public virtual bool MyProperty { get; set; } } public class ScheduleTypeMap : ClassMap...

pass specific enum value to method in objective-c

Altough I already know it's not possible, as my understanding how programming works, I would like to have a confirmation of this. I have an enum typedef enum { enum_first=1, enum_second=2 } myenum I wanted to do overloading of method, this syntax is obviously wrong, but it gives the idea: -(id)myenumTest:(myenum.enum_first)value {.....

Using default in a switch statement when switching over an enum

What is your procedure when switching over an enum where every enumeration is covered by a case? Ideally you'd like the code to be future proof, how do you do that? Also, what if some idiot casts an arbitrary int to the enum type? Should this possibility even be considered? Or should we assume that such an egregious error would be cau...

How do I write a generic for loop for a Java Enum?

Is there a way to write a generic loop to iterate over an arbitrary Enum? For example: public static void putItemsInListBox(Enum enum, ListBox listbox){ for(Enum e : enum.values(){ listBox.addItem(e.toString(), String.valueOf(e.ordinal()); } } You can not do the above, because the Enum class does not have a method call...

Iterate enum values using java generics

I'm trying to find a way to iterate through an enum's values while using generics. Not sure how to do this or if it is possible. The following code illustrates what I want to do. Note that the code T.values() is not valid in the following code. public class Filter<T> { private List<T> availableOptions = new ArrayList<T>(); p...

Use enum to select string from wicket properties file

I'd like to add a label to a wicket panel where the label's model is an enum value. Based on the value of that enum, I'd like to display a different message pulled from the page's properties file. For example, here's an enum: public enum ApprovalType { UNAPPROVED, APPROVED, BLOCKED }; I can easily add a label to the panel that has t...

Is there a way to iterate and reflect the member names and values contained in enumerations?

Let's say I have the following enum: public enum Colors { White = 10, Black = 20, Red = 30, Blue = 40 } I'm wondering if there is a way to iterate through all the members of Colors to find the member names and their values. ...

Working with enum-like data in C++

I am updating an old piece of C++ code and am stuck on a design issue and need advice on the best course of action. The code handles geometric data. Currently, the code defines many global constants to handle element types: #define TETRAHEDRON 0 #define HEXAHEDRON 1 Each constant has information associated with it that remains const...

How to pass an enum to Html.RadioButtonFor to get a list of radio buttons in MVC 2 RC 2, C#

I'm trying to render a radio button list in MVC 2 RC 2 (C#) using the following line: <%= Html.RadioButtonFor(model => Enum.GetNames(typeof(DataCarry.ProtocolEnum)), null) %> but it's just giving me the following exception at runtime: Templates can be used only with field access, property access, single-dim...

Bitwise operations on Java Enumerations; RE: Chess E.G.

If I were keeping an array in C representing a chess board, I might fill it with enumed items that roughly look like: enum CHESSPIECE { none = 0, pawn, knight, bishop, rook, queen, king, type_mask = 7, white = 8, white_pawn, white_knight, white_bishop, white_rook, white_queen, white_king, black = 16, black_pawn, black_kig...

How to use Enum with aditional options (All, None)

I have an enum, which: is included in my class as a property it represents some values from a database table (a couple of types) it is displayed in DropBox, so that it can be used as a filter Now I would like to add 'All' (or 'None' for example) value to this DropBox. How should I do this: add 'All' value to Enum? add 'All' value...

C# enum addition

greetings, i have the following enum: public enum LegalShipTypes : byte { Frigate = 1, Cruiser = 2, Destroyer = 3, Submarine = 4, AircraftCarrier = 5 } i was wondering if there is a way to get the total value of enum in any way. for instance this would result in (1 + 2 + 3 + 4 + 5) =...

LINQ & Enums as IQueryable

I basically have an enum public enum WorkingDays { Monday, Tuesday, Wednesday, Thursday, Friday } and would like to do a comparison against an input, which happens to be a string //note lower case string input = "monday"; The best thing I could come up with was something like this WorkingDays day = (from d in Enum....

In a web site, Where do you declare project-scope enums ?

Hi all In an asp.net web site project, Where do you declare project-scope enums ? ...

If I add new possible values to my enum type, will this change my wsdl ?

Hi, I have a web service and I'm using an enum type in it. I have made some changes in my code and i added a new value to my enum type. Is this going to change my wsdl declaration ? And is that going to break all the clients that use my web service ? I use .NET Thanks in advance! ...

Will `typedef enum {} t` allow scoped enum element identifiers in C++0x?

I believe the new C++ standard allows for an extra "scope" for enumerated types: enum E { e1, e2 }; E var = E::e1; Since I know lots of source files containing the old C-style enum typedef, I wondered if the new standard would allow using the typedef for these otherwise anonymous enumerated types: typedef enum { d1, d2 } D; D var = ...

Can I access the values of an enum class from a JSP using EL?

I have an enum class USState. I would like to iterate through the states in a JSP. Is it possible to access a list of USStates without first setting such a list as an attribute? It seems that something as static as an enum should always be available, but I can't figure out how to do it. Here's what I'm looking for: (except working) ...