enums

Finding the Highest Value in an Enumeration

I'm writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>()); I need this on two different enumerations, so I turned it into a generic method: /// <summary>Returns the highest value encountered in an en...

Passing an enum value as a stored procedure (data function) parameter in LINQ to SQL

As mentioned in this question, "LINQ to SQL allows table mappings to automatically convert back and forth to Enums by specifying the type for the column - this works for strings or integers." However, I am having trouble with this when using stored procedures (instead of auto-generated SQL) to create and update entities. In my database...

Comparing Java enum members: == or equals() ?

I know that Java enums are compiled to classes with private constructors and a bunch of public static members. When comparing two members of a given enum, I've always used .equals(), e.g. public useEnums(SomeEnum a) { if(a.equals(SomeEnum.SOME_ENUM_VALUE)) { ... } ... } However, I just came across come code tha...

IEnumerable Extension Methods on an Enum

I have an enum(below) that I want to be able to use a LINQ extension method on. enum Suit{ Hearts = 0, Diamonds = 1, Clubs = 2, Spades = 3 } Enum.GetValues(...) is of return type System.Array, but I can't seem to get access to a ToList() extension or anything else of that sort. I'm just looking to write something like...

What's the best way to return multiple enum values? (java and C#)

more of original content deleted to make question easier to reference: So I have a House class that has a method House.buy(Person p), causing the person to buy the house. I want to know if its possible for the Person to buy the House, so I also have a method House.tryBuy(Player p) that returns if the Person can buy the house. I have an ...

advantage of creating a temporary buffer when iterating over and modifying a collection in java

I am iterating over, and modifying a map (which is created from an existing group of enum objects) like the following: public class Dispenser { private Map<Ingredient, Integer> availableIngredients = new EnumMap<Ingredient, Integer>(Ingredient.class); public void orderSandwich(SandwichType sandwichType) { Map<Ingre...

Casting ints to enums in C#

There is something that I cannot understand in C#. You can cast an out-of-range int into an enum and the compiler does not flinch. Imagine this enum: enum Colour { Red = 1, Green = 2, Blue = 3 } Now, if you write: Colour eco; eco = (Colour)17; The compiler thinks that’s fine. And the runtime, too. Uh? Why did the C# te...

How to count all the enums in a SQL table.

Okay so I have a database field called moderated It is an ENUM with 3 values: approved denied unmoderated How can I write a query that counts the amount of each, so I can generate this output: Approved: 3 Denied: 10 Unmoderated: 23 ...

[C++] Enum declaration inside a scope that is a parameter of a macro.

Hi, I am trying to create a macro that takes a scope as a parameter. I know, it is probably not a good thing etc etc. I was trying this and got the problem that preprocessor looks for commas and parentheses... the problem is with enum. How would I declare a enum inside a scope that is a parameter of a macro? when the compiler see ...

How can I add some Enum values to a combobox

In the following example I would like to add flavours that start with "APPLE" to a ComboBox on a form. When the enums have unique values it works fine; however, in my example two enums PINEAPPLE_PEACH and APPLE_ORANGE both have a value of 1 and this messes up the results. Is it erroneous to have two enums with the same value and, if so...

PostgreSQL - Updating an Enum Type

I've got a field that is using an enumeration type. I wish to update the enum to have an additional field (I don't want to delete anything, just add a new label). What is the simplest way to do this? ...

Grails bind request parameters to enum

Hi, My Grails application has a large number of enums that look like this: public enum Rating { BEST("be"), GOOD("go"), AVERAGE("av"), BAD("ba"), WORST("wo") final String id private RateType(String id) { this.id = id } static public RateType getEnumFromId(String value) { values().find {it.id == val...

Boost::Random and Enumerated Types

Right now I'm generating a random enumerator using boost's random library. Basically I'm using an implicit conversion to specify the random generator's distribution, getting a random number, and then casting that back to the enumerated type. Ex: (minColor and maxColor are parameters of the enumerated type) boost::mt19937 randGen(std::t...

Seemingly basic C++ question

Alright, so this is annoying the hell out of me and I'm sure its a simple thing to do. Basically, I'm working with an open source C++ client called POCO to make a email client for a class... Basically, I have a pop3 client object that retrieves emails from my email server, and then puts the emails in an object called MailMessage. Now, I...

Sort Hashtable Data in ASP.NET C#

Hi ALL, I am binding my dropdownlist with Enum I have following enum and code for bind dropdownlist. public enum DignosisOrderType { All = 0, General = 1, Uveitis = 2, Coag =3, PreOp=4, Tests=5, RP =6 } public static void BindDropDownByEnum(DropDownList dropDownList, Type ...

Abstract enum a annotation attribute type

Hi, I have an interface which multiple enums are implementing, i.e public interface MinorCodes { public abstract int code(); public abstract String description(); } public enum IdentityMinorCodes implements MinorCodes { IDENTITY_UPLOAD_PICTURE_CODE(1, "Error while trying to upload a picture."), } Now I want to have a cus...

C# Enums with Flags Attribute

I was wondering if Enums with Flag attribute are mostly used for Bitwise operations why not the compilers autogenerate the values if the enum values as not defined. For eg. [Flags] public enum MyColor { Yellow = 1, Green = 2, Red = 4, Blue = 8 } It would be helpful if the values 1,2,4,8 are autogenerated if they are ...

Sharing enums with entities and services, keep in one layer or both?

So there is an entity that has an enum property and a wcf service that would return or take that enum type. Should the enum be in the entities assembly and mark it up with attributes or should I duplicate the enums for both assemblies, marking the service enums and leaving the entity enums undecorated? I don't like duplicating the enum t...

C# Enums: Nullable or 'Unknown' Value?

If I have a class with an enum member, and I want to be able to represent situations where this member is not defied, which is it better: A) Declare the member as nullable in the class using nullable types. E.g.: public SomeEnum? myEnum; B) Add a default, 'unknown' value to the enumeration. E.g.: public enum SomeEnum { Unknow...

using switch statements with constants or enumerations? (Which is better)? C#

HI, I've got a simple question, but one that has been bugging me for a while. Question: When using switch statements in C#, is it considered better practice to use enums over constants or vice versa? Or is it a matter of preference? I ask this because many people seem to like using enums, but when you are switching on an int value, you...