Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of Enum value

Tags:

I'm trying to make a function where we can get the Namevalue of a EnumValue

For example:

Get_Enum_ValueName(DayOfWeek, 0) 

...This will return "Sunday".

But my code don't works, it says the type is not defined:

Private Function Get_Enum_ValueName(Of T)(ByVal EnumName As T, ByVal EnumValue As Integer) As String     Return DirectCast([Enum].Parse(GetType(EnumName), EnumValue ), EnumName).ToString End Function 
like image 656
ElektroStudios Avatar asked Apr 16 '13 13:04

ElektroStudios


People also ask

How do you find the value of an enum?

To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

What is enum name?

The name() method of Enum class returns the name of this enum constant same as declared in its enum declaration. The toString() method is mostly used by programmers as it might return a more easy to use name as compared to the name() method.

What does enum valueOf return?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Can enum string value?

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.


2 Answers

Given an enum

public enum Week {     Monday,     Tuesday,     Wednesday,     Thursday,     Friday,     Saturday,     Sunday } 

here are the things you can do:

static void Main(string[] args) {      // enum to int     int i=(int)Week.Thursday;      // int to enum;     Week day=(Week)3;      // enum to string     string name=Week.Thursday.ToString();     string fun=Enum.GetName(typeof(Week), 6);     string agh=Enum.GetName(typeof(Week), Week.Monday);     string wed=EnumName(Week.Wednesday);      // string to enum     Week apt=(Week)Enum.Parse(typeof(Week), "Thursday");      // all values of an enum type     Week[] days=(Week[])Enum.GetValues(typeof(Week));      // all names of an enum type     string[] names=Enum.GetNames(typeof(Week));  }  static string EnumName<T>(T value) {     return Enum.GetName(typeof(T), value); } 

Edit 1

If you want to convert from one enum to another enum of different type based on the underlying numeric value (convert to integer and from integer), then use the following:

/// <summary> /// Casts one enum type to another based on the underlying value /// </summary> /// <typeparam name="TEnum">The type of the enum.</typeparam> /// <param name="otherEnum">The other enum.</param> public static TEnum CastTo<TEnum>(this Enum otherEnum) {     return (TEnum)Enum.ToObject(typeof(TEnum), Convert.ToInt32(otherEnum)); } 

to be used as

public enum WeekEnd {     Saturday = Week.Saturday,     Sunday = Week.Sunday }  static void Main(string[] args) {     var day = WeekEnd.Saturday.CastTo<Week>();     // Week.Sunday } 
like image 158
John Alexiou Avatar answered Sep 28 '22 22:09

John Alexiou


In C#, that would be:

return Enum.ToObject(typeof(T), EnumValue).ToString(); 

or (equally):

return ((T)(object)(EnumValue)).ToString(); 
like image 34
Marc Gravell Avatar answered Sep 28 '22 22:09

Marc Gravell