Solved

Get value of enumeration as string

Posted on 2008-10-14
3
727 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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