Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 502129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:15:37+00:00 2026-05-13T06:15:37+00:00

This is my enum: public enum DocumentTypes { [EnumMember] TYPE_1 = 1, [EnumMember] TYPE_2

  • 0

This is my enum:

public enum DocumentTypes
    {
        [EnumMember]
        TYPE_1 = 1,
        [EnumMember]
        TYPE_2 = 2,
        [EnumMember]
        TYPE_3 = 3,
        [EnumMember]
        TYPE_4 = 4,
        [EnumMember]
        TYPE_5 = 5,
        [EnumMember]
        TYPE_6 = 6,
        [EnumMember]
        TYPE_7 = 7,
        [EnumMember]
        TYPE_8 = 12

    }

If I want to obtain ‘TYPE_8’, if I only have 12, is there a way to get the enum value?

I tried this:

((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString()

which returns a value of ‘TYPE_4’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:15:37+00:00Added an answer on May 13, 2026 at 6:15 am
    string str = "";
    int value = 12;
    if (Enum.IsDefined(typeof (DocumentTypes),value))
         str =  ((DocumentTypes) value).ToString();
    else
         str = "Invalid Value";
    

    This gives will also handle invalid values trying to be used, without the internal exception being thrown

    You can also replace the string with DocumentTypes, ie

    DocumentTypes dt = DocumentTypes.Invalid; // Create an invalid enum
    if (Enum.IsDefined(typeof (DocumentTypes),value))
       dt = (DocumentTypes) value;
    

    And for the bonus point, here is how to add a custom string to an enum (copied from this SO answer)

    Enum DocumentType
    { 
        [Description("My Document Type 1")]
        Type1 = 1,
        etc...
    }
    

    Then add an extenstion method somewhere

    public static string GetDescription<T>(this object enumerationValue) where T : struct
    {
        Type type = enumerationValue.GetType();
        if (!type.IsEnum)
            throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
    
        //Tries to find a DescriptionAttribute for a potential friendly name
        //for the enum
        MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
        if (memberInfo != null && memberInfo.Length > 0)
        {
            object[] attrs = memberInfo[0].GetCustomAttributes(typeof (DescriptionAttribute), false);
    
            if (attrs != null && attrs.Length > 0)
            {
                //Pull out the description value
                return ( (DescriptionAttribute) attrs[0] ).Description;
            }
        }
        //If we have no description attribute, just return the ToString of the enum
        return enumerationValue.ToString();
    }
    

    Then you can use:

    DocumentType dt = DocumentType.Type1;
    string str = dt.GetDescription<DocumentType>();
    

    Which will retrive the Description attribute value.


    Edit – updated code

    Here is a new version of the extension method that does’t need to know the type of the Enum before hand.

    public static string GetDescription(this Enum value)
    {
        var type = value.GetType();
    
        var memInfo = type.GetMember(value.ToString());
    
        if (memInfo.Length > 0)
        {
            var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attrs.Length > 0)
                return ((DescriptionAttribute)attrs[0]).Description;
        }
    
        return value.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 299k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, I solved it: Dim htmlDoc As HtmlDocument = wb.Document… May 13, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer In the first case the javascript file is included on… May 13, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer You use forward declarations, but you anyway include the .h… May 13, 2026 at 7:42 pm

Related Questions

I'm asking this question despite having read similar but not exactly what I want
Why can't enum's constructor access static fields and methods? This is perfectly valid with
I want to serialize my enum-value as an int, but i only get the
I've seen several questions related to properly mapping an enum type using NHibernate. This
For example, I have two elements in an enum. I would like the first

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.