26

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

3 Answers3

42

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
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • 1
    Fell upon this article explaining pros and cons for .ToString() and .GetName(): http://blog.reneorban.com/2012/01/how-to-get-string-name-of-enum-and-why.html – SvendK May 04 '16 at 14:06
  • 1
    @SvendK: The link is broken. [This](https://stackoverflow.com/questions/37895901/enum-getname-vs-enum-tostring) question is handling the pros and cons of `.ToString()` and `.GetName()`. – roland Apr 20 '20 at 10:43
10

In C#, that would be:

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

or (equally):

return ((T)(object)(EnumValue)).ToString();
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
9
public enum WeekDay
{
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7
}

string s = WeekDay.Friday.ToString();

simple as that... unless I am misunderstanding something?

And if you only have the number:

string s = ((WeekDay)4).ToString();

UPDATE

OK, next time you should mention that you want something generic.. to use for all enums and not just that specific example. You can try this:

public static class EnumExtensions
{
    public static T ToEnum<T>(this int value) where T : struct
    {
        return (T)(object)value;
    }

    public static string ToEnumName<T>(this int value) where T : struct
    {
        return ((T)(object)value).ToString();
    }
}

Use like this:

int someEnumValue = 4;
string name = someEnumValue.ToEnumName<WeekDay>();

or:

WeekDay weekDay = someEnumValue.ToEnum<WeekDay>();

I still don't think that is really necessary though, because you still need to know the type of enum anyway... so therefore:

This: string name = ((WeekDay)someEnumValue).ToString();

and this string name = someEnumValue.ToEnumName<WeekDay>();

are equivalent... but.. whatever suits you.

Matt
  • 6,787
  • 11
  • 65
  • 112
  • That's easy as you said but my problem was about how to parse a enum name as an argument to a function which would returns that string! thanks – ElektroStudios Apr 16 '13 at 15:47
  • @ElektroHacker, see my update. I have a better understanding now that I realize you want something to work with ALL enums. Have fun. – Matt Apr 17 '13 at 03:18
  • Is the mentioned "ToEnumName()" an extension method? I cannot find it. – Taersious Nov 29 '18 at 21:32
  • @Taersious Yes it is and the definition for it is already in my answer above - just a few lines above where you were looking at how to use it. – Matt Dec 21 '18 at 05:04