Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I have declared enum like this
C#
enum monthToVal
        {
            JAN=1,FEB,MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
        }

Is it possible to take FEB from enum?

string monthName = monthToVal(2) or monthToVal(DateTime.Now.Month) or something like this?

Like if I pass position value then I want respective item from it.
I wonder why I did'nt use enum earlier....

Regards,
RK
Posted

You can play with enums this way:
C#
monthToVal value = (monthToVal)2; // value will hold monthToVal.FEB
string name = monthToVal.ToString("g"); // name will hold "FEB"
 
Share this answer
 
Refer- Enum.GetName Method[^].
You can do like below to get the Enum constant name from value.
C#
Enum.GetName(typeof(monthToVal), 2);
 
Share this answer
 
You did not check out the documentation[^], did you?
 
Share this answer
 
Why not do this all the correct way - with a data array?

Create a structure for all your desired data, such as:
struct YearParts { 
  int monNum;  // Month Number 1-12
  int monSize; // Days In Month (don't forget to test for leap-year!)
  string monShort; // Three-char month name
  string monLong;  // Full Month Name
  bool leapYr;
};


Assign them to an array - you may even make the array length 13, and make your Jan as index 1.

You could, if you wish, still use your enum as an alternative way to select an index.

They could be made public, put in an include file, and be available everywhere as your personal standard.

 
Share this answer
 
Comments
♥…ЯҠ…♥ 18-Nov-13 9:23am    
Thanks, always there is an alternate options exist in this programming world.
No issues, I got required answers from above solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900