.Net get Enum text from Enum or Get Enum Text from ENum Number

I am working on a web-service project, which has lots of property is set to use Enum. After I made a web-service call, some of the response properties is assigned by Enum value. I need to display the information to my windows application/web-site. When I show the value to the website, it displays the Enum number/integer rather then a string.

At the end, I have worked out how to display the Enum string on to my application.

Here it is my solution.
E.g. EnumVariable is the Enum that return by the web-service
Instead of using

txt_ToDisplay.Text = EnumVariable
Or
txt_ToDisplay.Text = EnumVariable.Value

You would need to use
txt_ToDisplay.Text = EnumVariable.toString()

On the other hand, if you need to convert the Enum Integer to Enum string
You can try the following.
VB.Net
DirectCast([Enum].Parse(GetType(EnumDataType), EnumVariable), EnumDataType)
C#
(EnumDataType))Enum.Parse(typeof(EnumDataType)), EnumVariable);

Hope your find it useful.

Leave a Reply