2
\$\begingroup\$

I want to use the enum on the client for a dropdown list. Does this make sense, or is there a smarter way of doing this?

public class clientEnum 
{
    public string Value { get;set;}
    public int Key { get; set; }
}

public static List<clientEnum> EnumToclientEnum <T>(T enumType) where T : Type
{
    List<clientEnum> list = new List<clientEnum>();

    foreach (var value in Enum.GetValues(enumType))
    {
        var name = value.ToString();              
        var number = (byte)Enum.Parse(enumType, name);
        list.Add(new clientEnum { Key = (int)number, Value = value.ToString() });
    }
    return list;
}
\$\endgroup\$
1
  • \$\begingroup\$ I assume localization is no problem for this application? \$\endgroup\$ – Myrtle Aug 2 '13 at 13:21
1
\$\begingroup\$

Your code won't work, I get an error at following line:

var number = (byte)Enum.Parse(enumType, name);

This is the error:

InvalidCastException: Specified cast is not valid.

You should leave the cast to a byte away. I'm no expert at enumerations so I went looking on the internet a bit and I think I found a solution. Also, I replaced the second value.ToString() with name when adding it to the list. No need for doing the same things twice.

public static List<clientEnum> EnumToclientEnum <T>(T enumType) where T : Type
{
    List<clientEnum> list = new List<clientEnum>();

    foreach (var value in Enum.GetValues(enumType))
    {
        var name = value.ToString();              
        var number = Enum.Parse(enumType, name);
        list.Add(new clientEnum { Key = (int)number, Value = name });
    }
    return list;
}

Taking things a bit further I'd make the method an IEnumerable<T> using yield return ... and when needed you can convert it to a List<T> when needed. Here, I left out putting the number in a variable as it used only once.

public static IEnumerable<clientEnum> EnumToclientEnum <T>(T enumType) where T : Type
{
    foreach (var value in Enum.GetValues(enumType))
    {
        var name = value.ToString();              
        yield return new clientEnum { Key = (int)Enum.Parse(enumType, name), Value = name };
    }
}

And if you care for short code:

public static IEnumerable<clientEnum> EnumToclientEnumShort <T>(T enumType) where T : Type
{
    foreach (var value in Enum.GetValues(enumType))
        yield return new clientEnum { Key = (int)Enum.Parse(enumType, value.ToString()), Value = value.ToString() };
}

Usage:

public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
var result = EnumToclientEnum(typeof(Days)); //IEnumerable
var listified = result.ToList(); //List
\$\endgroup\$
2
  • \$\begingroup\$ the underlying type for the enum i am using is a byte not an int. i guess checking for underlying type will make this better. thanks. \$\endgroup\$ – user1852503 Jul 31 '13 at 0:27
  • \$\begingroup\$ Ok didn't know that :) But the rest of the code might still be of help. ;) \$\endgroup\$ – Abbas Jul 31 '13 at 8:03
3
\$\begingroup\$

Yes, use linq extensions:

public static List<clientEnum> EnumToclientEnum<T>()
{
    var type = typeof(T);
    if (!type.IsEnum) throw new NotSupportedException();
    return Enum.GetValues(type).Select(x => new clientEnum { Key = (int)x, Value = x.ToString() }).ToList();
}

In your original code (as well as in Abbas's answer) enum value is being converted to string and then this string is parsed back to get the value. That makes no sense.

\$\endgroup\$
1
\$\begingroup\$

If you are open to use a framework you can use json.net and use the StringEnumConverter Class.

Or you could manually create it like this too.

string.Join(", ", Enum.GetNames(typeof(Days )).ToList().ConvertAll(key =>
{
    return string.Format("{0}: {1}", key, (int)((Days )Enum.Parse(typeof(Days ), key)));
}).ToArray()));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.