Home How do I resolve a string to the corresponding enum entry in C#?
Reply: 3

How do I resolve a string to the corresponding enum entry in C#?

JosepB
1#
JosepB Published in 2018-02-14 11:23:22Z

This question already has an answer here:

  • How should I convert a string to an enum in C#? 19 answers

I have the following code in C#

 class Program
    {
        static void Main(string[] args)
        {

            string text;

            Console.WriteLine(fooClass.RunInfoRequestType.text2);

            Console.Out.WriteLine("\nPress [Enter] key to close ...");
            Console.ReadLine();

        }

        public static class fooClass
        {
            public enum RunInfoRequestType
            {

                text1 = 1,
                text2 = 2,
                text3 = 3,
                text4 = 4,
                text5 = 5,
                text6 = 6,
                text7 = 7,
                text8 = 8
            }

        }


    }

I would like to know how could I pass dynamically the string text value to the fooClass.RunInfoRequestType.text2.

For instance, set "text3" to the string text and pass the string to the fooClass.RunInfoRequestType.[text]. I don't want to change the enum at runtime. Just, pass the text string as a parameter to get the enum value.

Dabbas
2#
Dabbas Reply to 2018-02-14 11:26:52Z

If I got it correctly, you need to use string text value to get the corresponding enum item:

RunInfoRequestType item = (RunInfoRequestType)System.Enum.Parse(typeof(RunInfoRequestType), text);

You can put this code in a method:

public static GetRunInfoRequestType(string src) {
return (RunInfoRequestType)System.Enum.Parse(typeof(RunInfoRequestType), text);
}

This code is without checking the value of src if it's correct or not.

Liquid Core
3#
Liquid Core Reply to 2018-02-14 11:28:48Z

Enums are compile time constants. You cannot change them at run time.

JohnLBevan
4#
JohnLBevan Reply to 2018-02-14 11:49:05Z

Is this what you're after; i.e. a way to have a run time display value for each enum value?

//initialise a dictionary to map your enum values to their runtime labels
IDictionary<RunInfoRequestType,string> labels = new Dictionary<RunInfoRequestType,string>
{
    { RunInfoRequestType.text1, RunInfoRequestType.text1.ToString() }
    ,{ RunInfoRequestType.text2, RunInfoRequestType.text2.ToString() }
    ,{ RunInfoRequestType.text3, RunInfoRequestType.text3.ToString() }
    ,{ RunInfoRequestType.text4, RunInfoRequestType.text4.ToString() }
    ,{ RunInfoRequestType.text5, RunInfoRequestType.text5.ToString() }
    ,{ RunInfoRequestType.text6, RunInfoRequestType.text6.ToString() }
    ,{ RunInfoRequestType.text7, RunInfoRequestType.text7.ToString() }
    ,{ RunInfoRequestType.text8, RunInfoRequestType.text8.ToString() }
};


//set text3 to string text
labels[RunInfoRequestType.text3] = text;

If so, the above will do it, but you may want to opt for a non-enum solution; e.g.

public class RunInfoRequestType 
{
    public int Id {get; private set;}
    public string DisplayName {get; set;}
    protected static readonly IDictionary<int,RunInfoRequestType> values = new Dictionary<int,RunInfoRequestType>();
    protected RunInfoRequestType (int id, string displayName)
    {
        this.Id = id;
        this.DisplayName = displayName;
        values.Add(id, this);
    }
    public static readonly RunInfoRequestType Text1 = new RunInfoRequestType(1, "text1");
    public static readonly RunInfoRequestType Text2 = new RunInfoRequestType(2, "text2");
    public static readonly RunInfoRequestType Text3 = new RunInfoRequestType(3, "text3");
    public static readonly RunInfoRequestType Text4 = new RunInfoRequestType(4, "text4");
    public static readonly RunInfoRequestType Text5 = new RunInfoRequestType(5, "text5");
    public static readonly RunInfoRequestType Text6 = new RunInfoRequestType(6, "text6");
    public static readonly RunInfoRequestType Text7 = new RunInfoRequestType(7, "text7");
    public static readonly RunInfoRequestType Text8 = new RunInfoRequestType(8, "text8");

    public static implicit operator int(RunInfoRequestType runInfoRequestType)
    {
        return runInfoRequestType.Id;
    }

    public static implicit operator RunInfoRequestType(int id)
    {
        RunInfoRequestType runInfoRequestType ;
        values.TryGetValue(id, out runInfoRequestType);
        return runInfoRequestType ;
    }

    public override string ToString()
    {
        return this.DisplayName;
    }
}

//set text3's display name to string text
RunInfoRequestType.Text3.DisplayName = text;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.481069 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO