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
Given an
enum
here are the things you can do:
In C#, that would be:
or (equally):
simple as that... unless I am misunderstanding something?
And if you only have the number:
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:
Use like this:
or:
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.