?
Solved

Get value of enumeration as string

Posted on 2008-10-14
3
Medium Priority
?
741 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
[X]
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
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 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;
            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

Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

771 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