Solved

Get value of enumeration as string

Posted on 2008-10-14
3
726 Views
Last Modified: 2012-08-13
hi,

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??

thanks




0
Comment
Question by:Abu Hamdan
  • 2
3 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
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
        {
            [StringValue("SqlDbType.NVarChar")]
            Type = SqlDbType.NVarChar
        }

Open in new window

0
 
LVL 8

Accepted Solution

by:
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;
            else
            {
                //Look for our 'StringValueAttribute' 
                //in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                StringValueAttribute[] attrs =
                   fi.GetCustomAttributes(typeof(StringValueAttribute),
                                           false) as StringValueAttribute[];
                if (attrs.Length > 0)
                {
                    _stringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
                }
            }
 
            return output;
        }

Open in new window

0
 
LVL 7

Author Closing Comment

by:Abu Hamdan
ID: 31505825
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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