Enum to Int. Int to Enum . Best way to do conversion?

.net, c#, enums

Solution

Yes it will do exactly that except `Enum` should be `enum`

public enum Days
 {
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7
 }

To use Enum.Parse you MUST provide a string so if you want to cast from the int you'd have to go via a string which is ugly.

Days x = (Days)Enum.Parse(typeof(Days), "3");
Days y = (Days)Enum.Parse(typeof(Days), 3.ToString());

... both give you wednesday.

Problem

``` public Enum Days { Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7 } ``` Now I wanted to know how do I get the integer value of an enum and convert an integer value into enum Will ``` int dayNo = (int) Days.Monday; ``` change the value of dayNo to 1; and Will ``` Days day = (Days) 2; ``` assign Days.Tuesday to the variable day ?? How about is this the best way to do the parsing??

Original source