20

sorry for asking this question but i didn't found the right solution for this task:

I've got a Enum, which is named "myEnum" (MyEnum isn't known by the function) I Need to get the int value of a Value of myEnum

Example:
A Programmer named its enum "myEnum":

 public enum myEnum
 {
     foo = 1,
     bar = 2,
 }

my function should do the following: Get the Value of "foo" of "myEnum" by string

function should opened by:

 public int GetValueOf(string EnumName, string EnumConst)
 {

 }

so when Programmer A opens it by :

 int a = GetValueOf("myEnum","foo");

it should return 1

and when Programmer B has an Enum named "mySpace", wants to return "bar" with Value 5

int a = GetValueOf("mySpace","bar")

should return 5

how can i do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    What is the use case? What actual, real scenario are you trying to solve? – Oded Sep 04 '12 at 19:43
  • 1
    The guts of this are the same as: http://stackoverflow.com/questions/169116/how-to-create-enum-object-from-its-type-and-name-of-the-value?rq=1 – Joel Rondeau Sep 04 '12 at 19:45
  • @Oded: The use case: I Want a Library where you can specify your own Enumerator for an ArrayList, so that the Programmer can Grab Value from ArrayList by its Name, the Enumerations inside gets a ReadableName with its Value of the ArrayList (eg.: Foo = 1, Bar = 2, Name = 3, Cost = 4 etc.) – Daniel Alexander Karr Sep 04 '12 at 20:51
  • @DanielAlexanderKarr Why not just use a `Dictionary` with the enum as the key? – Reed Copsey Sep 04 '12 at 22:04
  • can you give me an example with Dictionary in Use? – Daniel Alexander Karr Sep 05 '12 at 16:31

4 Answers4

31

You can use Enum.Parse to do this, but you'd need the fully qualified type name of the Enum type, ie: "SomeNamespace.myEnum":

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);
}

Also note that this uses Convert.ToInt32 instead of a cast. This will handle enum values with underlying types which are not Int32. This will still throw an OverflowException, however, if your enum has an underlying value outside of the range of an Int32 (ie: if it's a ulong as the value is >int.MaxValue).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for it, will try it shortly. Is there a possibility to get the fully qualified type name like: GetValueOf(MyEnum.Domain + "." MyEnum.Name, MyEnum.Const.ToString()) ? To Help you Understand: I will write a Library with a function to iterate through an Array by given Enum and its Value by Int to get the Value of this Array – Daniel Alexander Karr Sep 04 '12 at 20:45
  • Thank you, that worked, now (i dont want to open another thread for it) i only need the Maximum Amount of Enumentries like: "public enum Foo { a=1, b=2, c=5, } --> response should be 3 (because there are 3 entries inside enum) and another function to get the maximumvalue inside the enum like: "public enum Foo { a=1, b=2, c=5 } --> response should be 5 – Daniel Alexander Karr Sep 05 '12 at 17:10
  • One critically important question do i still have: my project got the namespace "Cache", how do i get the enum "MyEnum" of namespace "MyProject" ? "Cache.MyEnum" works pretty well mate ;) – Daniel Alexander Karr Sep 05 '12 at 17:28
  • 1
    @DanielAlexanderKarr That should be a separate question - but you can use Enum.GetNames and Enum.GetValues to get the names + values. See: http://msdn.microsoft.com/en-us/library/system.enum.getnames.aspx and http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx – Reed Copsey Sep 05 '12 at 17:30
8

Please try

int result = (int) Enum.Parse(Type.GetType(EnumName), EnumConst);
nawfal
  • 70,104
  • 56
  • 326
  • 368
petro.sidlovskyy
  • 5,075
  • 1
  • 25
  • 29
2

I suppose you're trying to instance the enum from the string value (it's name) then I'll suggest you to get it members via reflection and then compare.

Please be aware reflection adds a bit of overhead.

Community
  • 1
  • 1
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
2

It's not clear to me if the name of the enum type must be specified as a string.

You need to use Enum.TryParse to get the value of the Enum. In combination with a generic method, you could do something like this:

public int? GetValueOf<T>(string EnumConst) where T : struct
{
    int? result = null;

    T temp = default(T);
    if (Enum.TryParse<T>(EnumConst, out temp))
    {
        result = Convert.ToInt32(temp);
    }

    return result;
}

To call it use this:

int? result = GetValueOf<myEnum>("bar");
if (result.HasValue)
{
    //work with value here.
}
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48