Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to cast integer to enum? [duplicate]

Tags:

c#

.net

casting

I got the following enum:

 public enum detallistaDocumentStatus {

    /// <remarks/>
    ORIGINAL,

    /// <remarks/>
    COPY,

    /// <remarks/>
    REEMPLAZA,

    /// <remarks/>
    DELETE,
}

then I got a class property of type detallistaDocumentStatus:

 public detallistaDocumentStatus documentStatus {
        get {
            return this.documentStatusField;
        }
        set {
            this.documentStatusField = value;
        }
    }

In the real life the user will send us a number (1, 2, 3 or 4) representing each enum value in the order they are declared.

so, is it possible to cast like this?

det.documentStatus = (detallistaDocumentStatus)3;

if not, how could I get the enum value using an integer as an index, we are using a lot of enums, so we want to do something generic and reusable

like image 978
franko_camron Avatar asked Jan 17 '12 18:01

franko_camron


People also ask

Can you cast an int to an enum?

You can explicitly type cast an int to a particular enum type, as shown below.

Can enum have duplicate values?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can enum have int values?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

Do enums have to be unique?

Enum names are in global scope, they need to be unique.


3 Answers

Yes, it's possible to cast Enum to int and vice versa, because every Enum is actually represented by an int per default. You should manually specify member values. By default it starts from 0 to N.

It's also possible to cast Enum to string and vice versa.

public enum MyEnum {     Value1 = 1,     Value2 = 2,     Value3 = 3 }  private static void Main(string[] args) {     int enumAsInt = (int)MyEnum.Value2; //enumAsInt == 2      int myValueToCast = 3;     string myValueAsString = "Value1";     MyEnum myValueAsEnum = (MyEnum)myValueToCast;   // Will be Value3      MyEnum myValueAsEnumFromString;     if (Enum.TryParse<MyEnum>(myValueAsString, out myValueAsEnumFromString))     {         // Put logic here         // myValueAsEnumFromString will be Value1     }      Console.ReadLine(); } 
like image 158
ken2k Avatar answered Sep 20 '22 23:09

ken2k


From the C# 4.0 Specification:

1.10 Enums

Enum values can be converted to integral values and vice versa using type casts. For example

int i = (int)Color.Blue;      // int i = 2; Color c = (Color)2;               // Color c = Color.Blue; 

One additional thing to be aware of is that you are permitted to cast any integral value in the range of the enum's underlying type (by default that's int), even if that value doesn't map to one of the names in the enum declaration. From 1.10 Enums:

The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

So, the following is also permitted with the enum in your example:

det.documentStatus = (detallistaDocumentStatus) 42; 

even though there's no enum name that has the value 42.

like image 22
Michael Burr Avatar answered Sep 22 '22 23:09

Michael Burr


Yes it is possible. I use the following ENUM

public enum AccountTypes
{
  Proposed = 1,
  Open = 2
}

then when I call it I use this to get the value:

(int)AccountTypes.Open

And it will return the int value that I need which for the above will be 2.

like image 31
Taryn Avatar answered Sep 21 '22 23:09

Taryn