enums

Enum vs Lookup table vs Enum reflection vs State pattern

The software I will be building will involve "application" switching between different status a lot. Certain tasks can be done depends on the status an application is at. I was thinking about using enum as the status public class Application { public int Id {get;set;} public Status {get;set;} } public enum Status { [Description("N...

Convert from enum ordinal to enum type

Hi, I've an enum type: ReportTypeEnum that get passed between methods in all my classes but I then need to pass this on the URL so I use the ordinal method to get the int value. After I get it in my other JSP page I need to convert it to back to an ReportTypeEnum so that I can continue passing it. How can I convert ordinal to the Rep...

Question on operators

I was reading about flag enums and bitwise operators, and came across this code: enum file{ read = 1, write = 2, readandwrite = read | write } I read somewhere about why there is a inclusive or statement and how there can't be an &, but can't find the article. Can someone please refresh my memory and explain the reasoning? Also, how ...

enumerated datatypes and gcc

I have a function in which one of the function arguments is an integer. During function invocation I am passing an enumerated datatype to this function. After building using gcc, any access to the INTEGER variable inside the function causes a segmentation fault. Sample code: void somefun (unsigned int nState) { switch (nState) // <...

Enums and Constants. Which to use when?

Hi all, Okay this may seem like a silly question but I'm fairly new to all this. I was doing some reading on enums and find them very similar to declaring constants. How would I know when to use a constant rather than an enum or vice versa. What are some of the advantages of using enums? I never used them before. I'm working with c#. ...

Enum Casting

The following code does not produce an exception but instead passes the value 4 to tst. Can anyone explain the reason behind this? public enum testing { a = 1, b = 2, c = 3 } testing tst = (testing)(4); ...

Should I assume two-elements enums will remain this way?

It happens quite a lot that I've a two member enum : enum E{ A, B } Let's say I want to assign a different value to i according to my enum value. should I write this : int i= (e== E.A)?0:1; or this : int i; if (e==E.A) i=0; else if (e==E.B) ...

What is the preferred method for handling unexpected enum values?

Suppose we have a method that accepts a value of an enumeration. After this method checks that the value is valid, it switches over the possible values. So the question is, what is the preferred method of handling unexpected values after the value range has been validated? For example: enum Mood { Happy, Sad } public void PrintMood(Mo...

Using enums as status returned status indicators in C#

Just wondering what the general consensus was about returning enums from methods indicating the status. The notion of returning codes (ints) is pretty innate in oldschool systems programming (C) but I am wondering if the methodology for indicating the status has changed. thanks Edit: I am aware that enums are basically int values. I'm...

.NET WPF XAML Namespace Mapping for Enum types

I'm binding a collection of my W3CErrorOrWarning type objects to controls in a WPF Window. One of its properties is named "Type". It is of type W3CErrorOrWarningType which is a simple Enum: Enum W3CErrorOrWarningType ValidationError ValidationWarning End Enum I'm trying to use it in this way... <Window ... xmlns:en...

C# Enums and casting

If you declare an enum in C#, the default type is automatically int. So then why in a case statement or other instances when using the enum do you have to explicitly recast in order to use the values? What's the point of having an underlying type if you have to explicitely case or am I just doing something wrong here? private enum MyE...

When should you map a column to Enum type in code

Hi all: Maybe this is a dumb question, but when should you map a column into a enum type. For instance, we all know that the column "Gender" which is represents as "M" or "F" in the database can be map to a enum called Gender. Since there are generally 2 gender :), we can be pretty sure that this enum will not need updating. However,...

Explain the generics used in the Enum declaration

Can someone please explain what this means? Enum<T extends Enum<T>> This seems like a circular definition, and I find it highly confusing to say the least. ...

When declaring an enum, should you force the type to byte for under 256 entities?

If you have an enum in your application and you only have a few items, should you for the underlying type to be the smallest possible type? enum smaller : byte { one, two, three }; ...

Enum Boxing and Equality

Why does this return False public enum Directions { Up, Down, Left, Right } static void Main(string[] args) { bool matches = IsOneOf(Directions.Right, Directions.Left, Directions.Right); Console.WriteLine(matches); Console.Read(); } public static bool IsOneOf(Enum self, params Enum[] values)...

Does Ruby Have Enums?

Duplicate of: http://stackoverflow.com/questions/75759/enums-in-ruby I am new to Ruby from a C# background, and I want to know if you can have enums in Ruby and if so, how is it done? ...

C#: How can I use implicit cast operator during object to type conversion?

HI! Here is my case: I have some value type which is wrapped into another type with appropriate implicit converters. If I cast wrapped type to an object and then try to get original value I can do that in two-step cast only. If simplified my code is as follows: public enum MyEnum : int { First, Second } public class Test<T> ...

In Java, are enum types inside a class static?

I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass the instance into the enum's method where I need it? public class Universe { public f...

Destroy the EnumBuilder After Creating an Enum?

I am trying to reduce the amount of memory my application uses. In my application, I use System.Reflection.Emit.EnumBuilder to create enumerations on the fly from information I receive at runtime. I only use the EnumBuilder to create an individual enumeration, and after that I have no further use for it. While using CLRProfiler, I not...

Why no non-integral enums?

Why is it that non-integral enums cannot be created? I want to know if this is a language design decision, or if there are issues with implementing this in the compiler. In other words, is it feasible to implement non-integral enums into the language, but there just isn't a justifiable need? Or if it isn't feasible but is justifiable, ...