Get value of enumeration as string

Posted on 2008-10-14
Last Modified: 2012-08-13

i have enumeration as:

     public enum myenum
            Type = SqlDbType.NVarChar

and in any other method i need when calling this enum get the string as : SqlDbType.NVarChar
 string ss = myenum.Type.tostring();

this should result : SqlDbType.NVarChar

but i am getting the result as "Type"

any ideas??


Question by:Abu Hamdan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 22710161
You can do what you want in that simple way. To create an enum and return a string value, you can do something like this
// Create a class to read the attribut that you set on Enum options.
public class StringValueAttribute : System.Attribute
        private string _value;
        public StringValueAttribute(string value)
            _value = value;
        public string Value
            get { return _value; }
And on your Enum you can have this:
public enum myenum
            Type = SqlDbType.NVarChar

Open in new window


Accepted Solution

tiagosalgado earned 250 total points
ID: 22710176
Sorry, don't know why not my all code pasted. After the enum add this too.

private static Hashtable _stringValues = new Hashtable();
        public static string GetStringValue(Enum value)
            string output = null;
            Type type = value.GetType();
            //Check first in our cached results...
            if (_stringValues.ContainsKey(value))
                output = (_stringValues[value] as StringValueAttribute).Value;
                //Look for our 'StringValueAttribute' 
                //in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                StringValueAttribute[] attrs =
                                           false) as StringValueAttribute[];
                if (attrs.Length > 0)
                    _stringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
            return output;

Open in new window


Author Closing Comment

by:Abu Hamdan
ID: 31505825

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question