locked
How find the enum name for corresponding value? RRS feed

  • Question

  • Hi,

    I have an enum like this

    public enum SearchNodes : int
            {
                NewSearch = 0,
                SavedSearch = 1,
                SavedSearch_Sub = 2,
                Last10Searches=3,
                Last10Searches_Sub = 4

            }


    I have one lable in the above in the page.

    I am filling the the data set with id's

    I want the enum name corresponding to the dataset values,becuse depending on the id i want change the lable title.

    Any one help me plz...

    Regards,
    venkat.

    venakt
    Tuesday, November 4, 2008 9:24 AM

Answers

  • Hi,

    Enum.GetName(Type enumType, object enumValue) might help you. This returns the name of the constant in specified enumeration.

    BR,
    Tomi
    • Proposed as answer by Tomi Airaksinen Tuesday, November 4, 2008 10:36 AM
    • Marked as answer by Zhi-Xin Ye Friday, December 5, 2008 7:52 AM
    Tuesday, November 4, 2008 10:36 AM
  • Hi,

    You can directly use following code just cast the integer values to enume type and then use the ToString method to get the name.

                SearchNodes search = (SearchNodes)1;
                Response.Write(search.ToString());
    Nayan Paregi | Microsoft Certified Technology Specialist (MCTS)
    • Marked as answer by Zhi-Xin Ye Monday, November 10, 2008 11:07 AM
    Tuesday, November 4, 2008 12:41 PM

All replies

  • Hi,

    Enum.GetName(Type enumType, object enumValue) might help you. This returns the name of the constant in specified enumeration.

    BR,
    Tomi
    • Proposed as answer by Tomi Airaksinen Tuesday, November 4, 2008 10:36 AM
    • Marked as answer by Zhi-Xin Ye Friday, December 5, 2008 7:52 AM
    Tuesday, November 4, 2008 10:36 AM
  • Hi,

    You can directly use following code just cast the integer values to enume type and then use the ToString method to get the name.

                SearchNodes search = (SearchNodes)1;
                Response.Write(search.ToString());
    Nayan Paregi | Microsoft Certified Technology Specialist (MCTS)
    • Marked as answer by Zhi-Xin Ye Monday, November 10, 2008 11:07 AM
    Tuesday, November 4, 2008 12:41 PM
  • I use a technique where I surround each value of the enum with an attribute, which contains a description of the value.  This description can even have spaces in it.  I then read this attribute when I want to display the description.  Let me know if this is what you need and if you want the code.
    • Proposed as answer by Iwan de Beer Thursday, July 16, 2009 6:59 AM
    Tuesday, July 14, 2009 7:20 PM
  • Hi Iwan

    That's exactly what I'm looking for! I'd like to see your code if it's still available :-)

    Wednesday, February 8, 2012 10:38 AM
  • Hi Iwan

    That's exactly what I'm looking for! I'd like to see your code if it's still available :-)

    @Jul,

    I make use of this  technique extensively.

    There's a few parts to it. I usually have an accompanying class for each of my enums with a couple of methods - one of which is called GetDescription()

    	public enum Status { [Description("Test Mode")]TestMode = 1, [Description("Open")] Open = 2, [Description("Closed")] Closed = 3, [Description("Reconciled")] Reconciled = 4, [Description("Pending Reconcilliation")] PendingReconcilliation = 5 }
    
            public class StatusTypes
            {
                public static string GetDescription(int i)
                {
                    foreach (var eventStatus in Enum.GetValues(typeof(Status)).Cast<Status>().Where(eventStatus => (int)eventStatus == i))
                    {
                        return eventStatus.GetDescription();
                    }
                    return "";
                }
            }

    The second part is a public extension method, also called GetDescription()

            public static string GetDescription(this Enum e)
            {
                var type = e.GetType();
                var memInfo = type.GetMember(e.ToString());
                if (memInfo.Length > 0)
                {
                    var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attrs.Length > 0)
                    {
                        return ((DescriptionAttribute)attrs[0]).Description;
                    }
                }
                return e.ToString();
            }

    and you call it like so

    var statusText = StatusTypes.GetDescription(1);


    Thursday, May 10, 2012 4:41 PM