.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
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.
DirectCast([Enum].Parse(GetType(EnumDataType), EnumVariable), EnumDataType)
(EnumDataType))Enum.Parse(typeof(EnumDataType)), EnumVariable);

Hope your find it useful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.