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?)

Community
  • 1
  • 1
Dumbo
  • 13,555
  • 54
  • 184
  • 288

4 Answers4

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    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. – Daniel Hilgarth Jan 23 '12 at 14:19
  • @DanielHilgarth: Fix it. Copy/paste error. thx – Tigran Jan 23 '12 at 14:22
  • 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. – Daniel Hilgarth Jan 23 '12 at 14:26
  • Thanks, it works...I am working with fixed strings inside domainupdown control so if I dont do any mistake there should be no exception. – Dumbo Jan 23 '12 at 14:28
  • It also should be `domainUpDown1.Text` :P – Dumbo Jan 23 '12 at 14:28
  • @Sean87: Are you displaying "Pre-Post" in your control or "PrePost"? If you display the first version, try selecting it and see if it still works. I am pretty sure it doesn't. – Daniel Hilgarth Jan 23 '12 at 14:36
  • I can just remove that dash from actual string out of domain updown...so no problem! – Dumbo Jan 23 '12 at 14:38
  • @Sean87: And why do you have the `StringValue` attribute in the first place then? – Daniel Hilgarth Jan 23 '12 at 14:40
  • 1
    Yes, I don't see how this addresses the issue of hyphens, or other arbitrary text in your display name. – Kir Jan 23 '12 at 14:46
  • @DanielHilgarth I wanted to go PRO and do that, now I give up and go with dash-less enums... – Dumbo Jan 23 '12 at 14:50
  • @Sean87: Why don't you just use the answer in the other question? The one that Guillaume linked to? It solves **exactly** your problem. – Daniel Hilgarth Jan 23 '12 at 14:55
  • @DanielHilgarth yeah thanks I already read it (after guillaume linked it) now my priority was to make it work as fast as possible and Tigran's answer does the job. – Dumbo Jan 23 '12 at 15:04
  • @DanielHilgarth: what I changed typeof(...) content, which was not correct. The issue OP faces, **to me** seems related to a simple cast, and according to its comments, *it is*. – Tigran Jan 23 '12 at 15:08
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;
}
Kir
  • 2,905
  • 2
  • 27
  • 44
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);
                    }
                }
Massimo Zerbini
  • 3,125
  • 22
  • 22
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.. 
MethodMan
  • 18,625
  • 6
  • 34
  • 52