2

I am stuck with a piece code There is a object, a field of which returns an enum (getter) the field is set by a function after reading from database. For example

Enum fiscalperiond
{
    Num1 = 12;
    Num2 = 13;
}

Class A
{

    Public Fiscalperiod
    {
        NumberOfFiscalPeriod {get;set;}
    }
}

Now I want to do this how do I do it

A a = new A();

int k = a.NumberOfFiscalPeriod;
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
user3048027
  • 387
  • 1
  • 5
  • 24

1 Answers1

5

Just cast the enum value to int.

A a = new A();

int k = (int)a.NumberOfFiscalPeriod;

The inverse is valid too, even if there is no enum value defined for that int value, for sample:

int i = 13; // int value
EFiscalPeriond f = (EFiscalPeriond) i; // converting int to enum
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194