enums

When to use Enum or Collection in Java

Under what circumstances is an enum more appropriate than, for example, a Collection that guarantees unique elements (an implementer of java.util.Set, I guess...)? (This is kind of a follow up from my previous question) Thanks! ...

size of ENUM columns in SQL?

How do databases (mySQL if a particular example is important) determine the size of the column needed to store an ENUM? Is it something simple like a single byte for less than 256 enum options, etc.? ...

C# Iterating through an enum? (Indexing a System.Array)

I have the following code: // Obtain the string names of all the elements within myEnum String[] names = Enum.GetNames( typeof( myEnum ) ); // Obtain the values of all the elements within myEnum Array values = Enum.GetValues( typeof( myEnum ) ); // Print the names and values to file for ( int i = 0; i < names.Length; i++ ) { pri...

namespaces for enum types - best practices

Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use 'larger' enum element names. Still, the namespace solution has to possible implementations: a dummy class with nested enum, or a full blown namespace. I'm looking for pro's and con's of all ...

Is there a correct way to avoid warnings when comparing two different enums?

When comparing enums that come from different sources such as those of the following code GCC emits warnings. Is there a way to avoid these warnings without c-style casts? struct Enumerator { enum { VALUE = 5 }; }; template<int V> struct TemplatedEnumerator { enum { VALUE = V }; }; if(Enumerator::VALUE == TemplatedEnumerator<5...

Are these appropriate practices when working with std::map?

I have some questions on using std::map: Is using an enum as the key in std::map a good practice? Consider the following code: enum Shape{ Circle, Rectangle }; int main(int argc, char* argv[]) { std::map strMap; // strMap.insert(Shape::Circle,"Circle"); // This will not compile strMap[Shape::Circle] = "Circle"; ...

Should static variables be replaced with enums?

So I was looking at some code that was checked in and I got all puzzled over: // Amount of days before cancellation can't be done enum Cancellation { Limit = 2 }; Asking the guy who checked it in he argued that it's much better to use enums instead of static variables, bettern than this: private static int CANCELLATION_LIMIT = 2; S...

Persisting data suited for enums

Most projects have some sort of data that are essentially static between releases and well-suited for use as an enum, like statuses, transaction types, error codes, etc. For example's sake, I'll just use a common status enum: public enum Status { ACTIVE(10, "Active"); EXPIRED(11, "Expired"); /* other statuses... */ /* c...

C# enums as function parameters?

Can you pass a standard c# enum as a parameter? For example: enum e1 { //... } enum e2 { //... } public void test() { myFunc( e1 ); myFunc( e2 ); } public void myFunc( Enum e ) { // Iterate through all the values in e } By doing this I hope to retrieve all the names within any given enum. What would the Iterat...

C# naming convention for enum and matching property

Hi All, I often find myself implementing a class maintaining some kind of own status property as an enum: I have a Status enum and ONE Status property of Status type. How should I solve this name conflict? public class Car { public enum Status { Off, Starting, Moving }; Status status = Status.Off; public Status ...

Converting enum tostring of underlying type in VB.Net (Option Strict On)

I'd like to get a string representation of the underlying type of the enum. Dim target As System.ConsoleColor = ConsoleColor.Cyan Dim actual = 'What goes here? Dim expected = "11" ...

Linq to Sql Enums and Conditional Operator

Whichever way I do it, it seems something goes wrong when using the conditional operator on enum values in Linq to Sql. For example var ret = from listings in db.Listings select new Listing { ID = listings.ID, //etc OrderStatus = listings.OrderItems.Count > 0 ? listings.OrderItems.First().Order.OrderStatus : OrderStatu...

C# int to enum conversion

If I have the following code: enum foo : int { option1 = 1, option2, ... } private foo convertIntToFoo(int value) { // Convert int to respective Foo value or throw exception } What would the conversion code look like? ...

In C#, can I use reflection to determine if an enum type is int, byte, short, etc?

Is this possible? I can't find it anywhere. Thanks! ...

How do I serialize an enum value as an int?

I want to serialize my enum-value as an int, but i only get the name. Here is my (sample) class and enum: public class Request { public RequestType request; } public enum RequestType { Booking = 1, Confirmation = 2, PreBooking = 4, PreBookingConfirmation = 5, BookingStatus = 6 } And the code (just to be sure i'm not doing it w...

Convert.ChangeType and converting to enums?

I got an Int16 value, from the database, and need to convert this to an enum type. This is unfortunately done in a layer of the code that knows very little about the objects except for what it can gather through reflection. As such, it ends up calling Convert.ChangeType which fails with an invalid cast exception. I found what I conside...

c# Enum Function Parameters

As a follow on from this question. How can I call a function and pass in an Enum? For example I have the following code: enum e1 { //... } public void test() { myFunc( e1 ); } public void myFunc( Enum e ) { var names = Enum.GetNames(e.GetType()); foreach (var name in names) { // do something! } } ...

Converting strings to enum in C++?

Strings to enum in C#, how do you normally converting strings to enum in C++. Any helper function that you use, is it a good idea to do this. ...

Abstract Enum selection box

I'm trying to make a function that can take an enum type, display all the possible selections to the user, let them select one and then pass it back. Generics don't allow you to limit to enum. I've got code working that will cast back and forth, but I'd like it to accept and return the same enum type. This code works but not as well as ...

How to create type-safe int - enum in C++?

I need to create many classes that are somewhere between integer and enum. I.e. have the arithmetics of integer but also are not implicitly converted to int. ...