Solved

Get value of enumeration as string

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now