Solved

Get value of enumeration as string

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

11 Experts available now in Live!

Get 1:1 Help Now