Get value of enumeration as string

Posted on 2008-10-14
Medium Priority
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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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