3

Possible Duplicate:
Cast string to enum with enum attribute

I have a enum like this:

public enum IddFilterCompareToCurrent
{
    [StringValue("Ignore")]
    Ignore,
    [StringValue("Pre-Post")]
    PrePost,
    [StringValue("Custom")]
    Custom
}

I also have some DomainUpDown controls that are filled with same values of that enum I defined, exept that because enum does not accept - character, I had to use Attributes to match them with DomainUpDown contents.

Now my question is how can I insert the selected item of a domainupdown into a variable of that enum type?

something like:

private IddFilterCompareToCurrent myEnum = Enum.Parse(typeof(IddFilterCompareToCurrent), domainUpDown1.SelectedItem.ToString());

I get this error:

Cannot implicitly convert type 'object' to 'Filtering.IddFilterCompareToCurrent'. An explicit conversion exists (are you missing a cast?)

2
  • @Mikant: It sure is. Why do you think it is not? Commented Jan 23, 2012 at 14:26
  • Sorry you're right. Just looked the wrong tab. Commented Jan 23, 2012 at 14:35

4 Answers 4

3

Do this:

private IddFilterCompareToCurrent myEnum = 
(IddFilterCompareToCurrent )Enum.Parse(typeof(IddFilterCompareToCurrent ),domainUpDown1.SelectedItem.ToString());

Enum.Parse returns an Object, so you need to cast it.

Sign up to request clarification or add additional context in comments.

13 Comments

This doesn't help much further. It will now result in an exception at runtime as soon as the attribute value doesn't match the enum value name.
@DanielHilgarth: Fix it. Copy/paste error. thx
I don't see what you changed, but this change didn't address the problem. Please have a look at the accepted answer in the question linked to in the duplicate comment.
Thanks, it works...I am working with fixed strings inside domainupdown control so if I dont do any mistake there should be no exception.
Yes, I don't see how this addresses the issue of hyphens, or other arbitrary text in your display name.
|
1

You need to create a wrapper object for enumerations that will read the StringValueAttribute

public class EnumWrapper<TEnum>
{
    public static EnumWrapper<TEnum>[] GetItems()
    {
        Type type = typeof(TEnum);
        var enumObjects = Enum.GetValues(type);
        var enumTyped = enumObjects.Select((v) => (TEnum)v);
        EnumWrapper<TEnum>[] ret = enumTyped.Select((e) => new EnumWrapper<TEnum>(e));

        return ret;
    }
}

public string DisplayName { get; private set; }
public TEnum  EnumValue { get; private set; }

private EnumWrapper(TEnum enumVal)
{
   Type type = enumVal.GetType();
   // Read attributes here. I'm leaving this out. if you need it, let me know.
   DisplayName = GetStringValueAttributeOfEnumValue(enumVal);
   EnumValue = enumVal;
}

Comments

1

You need to parse the value of the field name of the enum that is annotated with the attribute equal to the input value:

FieldInfo[] fields = typeof(IddFilterCompareToCurrent).GetFields();
                foreach (FieldInfo fi in fields) 
                {
                    object[] atts = fi.GetCustomAttributes(typeof(StringValueAttribute), false);
                    if (atts != null && atts.Length > 0) 
                    {
                        StringValueAttributeatt = atts[0] as StringValueAttribute;
                        if (att.Value == domainUpDown1.SelectedItem.ToString()) return (IddFilterCompareToCurrent)Enum.Parse(typeof(IddFilterCompareToCurrent), fi.Name);
                    }
                }

Comments

0

Can you alter the enum and do something like this.. it will return the names of the Enums if you do it this way.. otherwise what does the Actualy Class of [StringValue("..") look like..?

    enum IddFilterCompareToCurrent 
    { 

        Ignore,     
        PrePost,
        Custom 
    } 

usage: 
    var strEnumNames = Enum.GetNames(typeof(IddFilterCompareToCurrent));

returns a string[] "Ignore","PrePost","Custom";

 if you have this already working please ignore.. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.