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

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

user1468
1#
user1468 Published in May 23, 2018, 4:48 pm

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.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO