0

I know How to find enum value if enum name & key are known at compile time. I have a situation where I get the enum name at runtime. any suggestion how to achieve this.

using System;

namespace EnumDemo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string[] ArrItemNames = Enum.GetNames(typeof (EnumClass.Colors));
            foreach (string ItemName in ArrItemNames)
            {
                Console.WriteLine(
                    "{0} = {1:D}", ItemName,
                    Enum.Parse(typeof (EnumClass.Colors), ItemName));
            }
            Console.WriteLine();

            var EnumVal = GetEnumValue("Colors", "Red");// Here I am expecting 1
            Console.ReadKey();
        }

        //
        public static int GetEnumValue(string EnumName, string ItemName)
        {
            return 0;
        }
    }

    public class EnumClass
    {
        public enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
    }
}

Note: My enum is inside a class.

Rasmita Dash
  • 917
  • 5
  • 15
  • 28
  • this is nit-picking, but in c# parameters of methods should be lower case. – Staeff Apr 29 '15 at 08:51
  • I have tried the said answer there. But, it's not working. Type.GetType("EnumDemo.EnumClass.Colors") is giving null – Rasmita Dash Apr 29 '15 at 10:07
  • I think it's not a duplicate question. There enum is not inside any class. But, in my case it's inside a class. I am having issue just because my enum is inside a class. – Rasmita Dash Apr 29 '15 at 10:16
  • Since you seem to know the type of your enum anyways you could just go straight for the type, I've modified my answer accordingly – Staeff Apr 29 '15 at 10:20

2 Answers2

8

Use the Enum.Parse function and convert it to int as described here

public static int GetValueOf(string enumName, string enumConst)
{
    Type enumType = Type.GetType(enumName);
    if (enumType == null)
    {
        throw new ArgumentException("Specified enum type could not be found", "enumName");
    }

    object value = Enum.Parse(enumType, enumConst);
    return Convert.ToInt32(value);
}

If you are going to call this for your enum in your subclass you need to do it in this way:

public static void Main() {
{
    Console.WriteLine(GetValueOf("YourNamespace.EnumClass+Colors", "Red"));
}

Since you know the type you could also use this directly as:

public static void Main() {
{
    Console.WriteLine(GetValueOf(typeof(EnumClass.Colors), "Red"));
}


public static int GetValueOf(Type enumType, string enumConst)
{
    object value = Enum.Parse(enumType, enumConst);
    return Convert.ToInt32(value);
}
Community
  • 1
  • 1
Staeff
  • 4,994
  • 6
  • 34
  • 58
  • I think that if you can copy the answer from another question without changing anything you should mark this as a duplicate rather copy pasting the answer here. – RobH Apr 29 '15 at 08:50
  • It's working. There is some mistake(sort of typo) can you please change Console.WriteLine(typeof(EnumClass.Colors), "Red"); to Console.WriteLine(GetValueOf(typeof(EnumClass.Colors), "Red")); and Console.WriteLine("YourNamespace.EnumClass+Colors", "Red"); to Console.WriteLine(GetValueOf("YourNamespace.EnumClass+Colors", "Red")); . I can accept this as answer once you correct it. Thanks – Rasmita Dash Apr 29 '15 at 10:55
  • oh thanks! I just wrote it out of my head and there it made sense haha – Staeff Apr 29 '15 at 12:50
1

If I correctly understand the problem, the solution could be:

public static int GetEnumValue(string ItemName)
{
    return (int)Enum.Parse(typeof(EnumClass.Colors), ItemName);
}
insilenzio
  • 918
  • 1
  • 9
  • 23