enums

C# String enums.

Hi, I have the following enumerator: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION = 2, SINGLESIGNON = 3 } The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1. I have found the following solution for this problem: First I need to create a custom at...

Proper way to declare an enum in Managed C++ 2005?

If I use /clr:oldSyntax the following should work: public __value enum IceCreamFlavors { Vanilla, Chocolate, Sardine, }; what is the equivalent in non-oldSyntax? How do I declare a "managed" enum in Managed C++ for .NET 2.0? Edit: when I follow JaredPar's advice, then if I try to pass an IceCreamFlavor to a function with the...

Next or previous enum

Given an enum that has assigned values, what is the best way to get the next or previous enum given a value. For example, consider this enum: public enum TimeframeType { None = 0, [Description("1 month")] Now = 30, [Description("1-3 months")] Short = 90, [Description("3-6 months")] Medium = 180, [Descript...

.NET: Why aren't Enum's Range/Value Checked?

This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals can explain it to me. Suppose I define an enum as follows: public enum Foo { Eenie = 1, Meenie = 2, Miney = 3, Moe = 4 } Now, also suppose that somewhere in my code, I have the following code: int bar = (Foo)5; This will compile j...

using declaration with enum?

using declaration does not seem to work with enum type class Sample{ public: enum Colour { RED,BLUE,GREEN}; } using Sample::Colour; does not work!! do we need to add using declaration for every enumerators of enum type? like below using sample::Colour::RED; ...

How do send a Collection of Enums to a WCF Service

I am having trouble sending a collection of Enums to a WCF service method. I used this post as a guide: Sharing Enum with WCF Service [ServiceContract] [ServiceKnownType(typeof(MyEnum))] [ServiceKnownType(typeof(List<MyEnum>))] public interface IMyService{ [OperationContract] MyEnum ServiceMethod1( ); [OperationContract] ...

Indexing arrays with enums in C#

I have a lot of fixed-size collections of numbers where each entry can be accessed with a constant. Naturally this seems to point to arrays and enums: enum StatType { Foo = 0, Bar // ... } float[] stats = new float[...]; stats[StatType.Foo] = 1.23f; The problem with this is of course that you cannot use an enum to index a...

Why can't enum's constructor access static fields?

Why can't enum's constructor access static fields and methods? This is perfectly valid with a class, but is not allowed with an enum. What I'm trying to do is store my enum instances in a static Map. Consider this example code which allows lookup by abbreivation: public enum Day { Sunday("Sun"), Monday("Mon"), Tuesday("Tue"), Wedne...

Is there any pattern or refactoring trick to split Enum and DBDictionary data duplicate

We have a smelly code in our project. A lot of values which used in biz logic have 2 places where they stored: we have dictionary tables (used in FK relations) in DB where we stored values e.x. MessageDirectionInfo: 0|Unknown 1|In 2|Out and we have Enum with exactly same data: MessageDirectionEnum{Unknown=0,In=1,Out=2} And all over ...

Best way to define error codes/strings in Java?

I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a numerical error code and an error string grouped together. Both the error code and error string will be sent to the client accessing the web service. For example, when a SQLException oc...

C# enumeration property null vs. 0

I'm using IIS/asmx to support a Flash client. Some of my service layer data transfer objects have properties that are enumeration values. There are cases where these properties should be null. When an object with a null value for such an enumeration property is rendered to soap, I receive this error: System.InvalidOperationExceptio...

Strange java enum bug

I have a really strange enum bug in Java. for(Answer ans : assessmentResult.getAnswersAsList()) { //originally stored in a table //AnswerStatus stat = ans.getStatus(); if (ans.getStatus() == AnswerStatus.NOT_ASSESSED) { assessed = false; } } An answer is an answer to a question on a test. An assessment result is th...

C# What is the best way to create an enum that is used by multiple classes?

Hi, I have an enum which is used by multiple classes. What is the best way to implement this? Thnx ...

C# enums and booleans - looking for a more elegant way

I have an class which has a enum property and a boolean property, based on that it calls a specific method with specific parameters. I use a switch statement for the enum and an if for the boolean within each case of the switch. It is a long list and doesn't feel to me to be the most elegant solution. Anyone got a more elegant or simpler...

C# vs Java Enum (for those new to C#)

I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#. I'm trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#'s enums seem to be more simplistic than the Java 1.5+ implementation. Can anyone enumerate the differences ...

Does Perl have an enumeration type?

Does Perl have an enumeration type that adheres to best practices, or maybe more importantly, does it need one? The project I am working one uses strings all over the place to denote things that would typically use an Enum in a language like C#. For example, we have a set of phone numbers in an array of hashes, each associated with a p...

Performance of enum conversion

Hi folks, after a bit speedtracing I found a piece of code (called very very often) which converts values of one enum to values of another enum, like this: public Enum2 ConvertToEnum2(Enum1 enum1) { switch(enum1) { case Enum1.One: return Enum2.One; break; case Enum1.Two: return Enum2.Two; ...

How to test for String = Enum.Value?

How do I do a simple compare of an enum value and a string that should match the enums name? How do I parse the string into it's appropriate enum value. For example, Enum A B=0 C=1 D=2 End Enum How do I check if String = A.C and how do I convert string into its corresponding A value without comparing it to a string re...

Can I add and remove elements of enumeration at runtime in Java

It is possible to add and remove elements from an enum in Java at runtime? For example, could I read in the labels and constructor arguments of an enum from a file? @saua, it's just a question of whether it can be done out of interest really. I was hoping there'd be some neat way of altering the running bytecode, maybe using BCEL or ...

Enum ToString

My enum consists of the following values: private enum PublishStatusses{ NotCompleted, Completed, Error }; I want to be able to output these values in a user friendly way though. In this SO post, there is a lot of code that I can't seem to compile.. I don't need to be able to go from string to value again. Why is it not compiling a...