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

  • SEARCH
  • Home
  • 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 99001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:18:14+00:00 2026-05-11T00:18:14+00:00

I have the following enumeration: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION =

  • 0

I have the following enumeration:

public enum AuthenticationMethod {     FORMS = 1,     WINDOWSAUTHENTICATION = 2,     SINGLESIGNON = 3 } 

The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1.

I have found the following solution for this problem (link):

First I need to create a custom attribute called "StringValue":

public class StringValue : System.Attribute {     private readonly string _value;      public StringValue(string value)     {         _value = value;     }      public string Value     {         get { return _value; }     }  } 

Then I can add this attribute to my enumeration:

public enum AuthenticationMethod {     [StringValue("FORMS")]     FORMS = 1,     [StringValue("WINDOWS")]     WINDOWSAUTHENTICATION = 2,     [StringValue("SSO")]     SINGLESIGNON = 3 } 

And of course I need something to retrieve that StringValue:

public static class StringEnum {     public static string GetStringValue(Enum value)     {         string output = null;         Type type = value.GetType();          //Check first in our cached results...          //Look for our 'StringValueAttribute'           //in the field's custom attributes          FieldInfo fi = type.GetField(value.ToString());         StringValue[] attrs =            fi.GetCustomAttributes(typeof(StringValue),                                    false) as StringValue[];         if (attrs.Length > 0)         {             output = attrs[0].Value;         }          return output;     } } 

Good now I’ve got the tools to get a string value for an enumeration. I can then use it like this:

string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS); 

Okay now all of these work like a charm but I find it a whole lot of work. I was wondering if there is a better solution for this.

I also tried something with a dictionary and static properties but that wasn’t better either.

  • 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. 2026-05-11T00:18:15+00:00Added an answer on May 11, 2026 at 12:18 am

    Try type-safe-enum pattern.

    public sealed class AuthenticationMethod {      private readonly String name;     private readonly int value;      public static readonly AuthenticationMethod FORMS = new AuthenticationMethod (1, 'FORMS');     public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod (2, 'WINDOWS');     public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod (3, 'SSN');              private AuthenticationMethod(int value, String name){         this.name = name;         this.value = value;     }      public override String ToString(){         return name;     }  } 

    Update Explicit (or implicit) type conversion can be done by

    • adding static field with mapping

      private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string,AuthenticationMethod>(); 
      • n.b. In order that the initialisation of the the ‘enum member’ fields doesn’t throw a NullReferenceException when calling the instance constructor, be sure to put the Dictionary field before the ‘enum member’ fields in your class. This is because static field initialisers are called in declaration order, and before the static constructor, creating the weird and necessary but confusing situation that the instance constructor can be called before all static fields have been initialised, and before the static constructor is called.
    • filling this mapping in instance constructor

      instance[name] = this; 
    • and adding user-defined type conversion operator

      public static explicit operator AuthenticationMethod(string str) {     AuthenticationMethod result;     if (instance.TryGetValue(str, out result))         return result;     else         throw new InvalidCastException(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer AFAIK Value types are put on the stack, reference types… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer First, go back and reread the memory management rules just… May 11, 2026 at 11:53 pm
  • Editorial Team
    Editorial Team added an answer I am not quite sure what you mean by "host… May 11, 2026 at 11:53 pm

Related Questions

This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
I have an enumeration value marked with the following attribute. The second parameter instructs
I have a WCF service operation that accepts a data contract parameter of custom
When compiling the following simpleType with the XJC compile (from the JAXB package)... <xs:simpleType

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.