views:

92

answers:

4

I have a enumerator which map to a bunch of int

example

enum MyEnum {
Open = 1,
Closed = 2,
Exit = 4

}

I find though that when I want to assign this to an integer, I have to cast it first.

int myEnumNumber = **(int)** MyEnum.Open;

Is it possible to specify the type of an enum so that it is implicit that there is a integer assigned to any value within the enum? That way, I do not need to keep casting it to an int if I want to use it

thanks

+2  A: 
enum MyEnum : int
{
    Open = 1,
    Closed = 2,
    Exit = 4
}

This is also mentioned here.

However, this does not allow you to avoid casting, this allows it to be used with types other than Int32 (which is the default enum type).

In short, yes, you can specify a type but no, you still have to cast it.

McAden
Does this remove the need for the cast though? That is what I think @RoboShop is wanting.
Ash
ah, no, I'll clarify
McAden
A: 

You can specify a different underlying type using:

enum : byte MyEnum { ... }

But this does not remove the need to cast to byte (if you really want to). Enums in C# allow you to normally just forget about the underlying type unless your applciation requires it.

Do you really need the underlying integer? It is possible to actually store a string representation of an enumeration value in a database and then recreate the enumeration value from the string at a later point, see this question.

MSDN gives a good tutorial on the do's and don't of enums.

Ash
+3  A: 

No, this is on purpose - enums have an underlying data type, but they are not considered to be identical, because this possibly creates lots of error possibilities that this way are simple to catch.

For example you say so much about having to cast the num all the time - I can not remember when I did do a cast of an enum last time. And I do a LOT of C# programming.

TomTom
sorry can you elaborate? Are you saying casting an enum to an INT is bad programming practice?
RoboShop
Well, give me examples what you need that for? ;) I normally use it:* Database mapping (one location ever).* Flag checks (helper class anyway, now there in 4.0 as a method)Anything else? Why would you ever cast it?
TomTom
oh ok. I'm using it because i'm getting a data extract from an excel file.I'm converting this to a datatable and I'm using the enum to access the Datacolumn and their mapping in relation to the data in the extract.eg. dr[(int)myEnum.FirstName] is much easier to understand then dr[4];Can you think of any better way of doing this?
RoboShop
+1  A: 

There are a few good reasons and probably a lot of bad ones for converting enum values to ints, I'll assume you have a good reason ;).

If you are doing a lot of int casting an extension method might be helpful, here is a quicky extension method:

public static int EnumCast(this Enum theEnum)
{
    return (int)((IConvertible)theEnum);
}

And an example of usage in a test:

[Test]
public void EnumCastTest()
{
    Assert.That(MyEnum.Exit.EnumCast(), Is.EqualTo(4));
}
Travis Brooks
I can't really see any benefit in doing this.You're writing more code than you need and an enum casting to an int is never going to fail anyway.
RoboShop