0

I am trying to get the description from an array of enums bascically this is the current structure that I have.

public enum IllinoisNonDisclosureConvictionFormOptions
{
    [Description("625 ILCS 5/11-501 - Driving Under the Influence")]
    answerConviction0 = 0,

    [Description("625 ILCS 5/11-503 - Reckless Driving")]
    answerConviction1 = 1,

    [Description("a violation of Article 11 of the Criminal Code of 1961, not including prostitution under 720 ILCS 5 / 11 - 14")]
    answerConviction2 = 2,

    [Description("720 ILCS 5/26-5 - Dog Fighting")]
    answerConviction3 = 3,

    [Description("720 ILCS 5/12-1 - Assault")]
    answerConviction4 = 4,
}

So basically a user will select the crime they committed and then that text will be compared in a if statement to the description of the enums. What I currently have is this:

    if (request.Question4 != null)
    {
        var answersForQuestion4 = Enum.GetValues(typeof(IllinoisNonDisclosureConvictionFormOptions)).Cast<IllinoisNonDisclosureConvictionFormOptions>().ToList();
        foreach (Enum answer in answersForQuestion4)
        {
            //I need to compare the description to the selected text
            string description = (enum description value)
            if (request.Question4 == description)
            {return description}
        }

    }

I may have to switch from enums to ControllersConstants since I dont really need to save their answers in the database. Please let me know if you have any insights regarding this matter.

Lostaunaum
  • 697
  • 1
  • 10
  • 31

3 Answers3

1

This posted question (Get Enum from Description attribute) includes getting the enum description.

If you need the opposite and know that the responses MUST match your enum description, you can work backwards to get the enum using Max's answer.

Community
  • 1
  • 1
Adam Milecki
  • 1,738
  • 10
  • 15
1

The solution you posted seems to be looking for a matching enum name (e.g. answerConviction0) instead of a matching description as described in the question.

You can do something like this:

string findMe = "625 ILCS 5/11-503 - Reckless Driving";

Type enumType = typeof(IllinoisNonDisclosureConvictionFormOptions);
Type descriptionAttributeType = typeof(DescriptionAttribute);

foreach (string memberName in Enum.GetNames(enumType))
{
    MemberInfo member = enumType.GetMember(memberName).Single();

    string memberDescription = ((DescriptionAttribute)Attribute.GetCustomAttribute(member, descriptionAttributeType)).Description;

    if (findMe.Equals(memberDescription))
    {
        Console.WriteLine("Found it!");
    }
}

Note that all this reflection will be slow. Maybe you would be better off with an array of strings instead of an enumeration with descriptions.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • Damm dude you are completely correct, one of the issues we discussed with the team was performance I may switch to defining these as ControllerConstants but since the user wont have direct access to this part and the performance wont be that greatly affected we decided to stick to enums. – Lostaunaum Sep 18 '15 at 19:25
0

Thank you so much for the positive feedback after reading through the posts I realized that my mistake was that answer was being set to an ENUM instead of an INT. I believe that this code format will give me the desired result:

if (request.Question4 != null)
{
    var answersForQuestion4 = Enum.GetValues(typeof(IllinoisNonDisclosureConvictionFormOptions)).Cast<IllinoisNonDisclosureConvictionFormOptions>().ToList();
    foreach (int answer in answersForQuestion4)
    {
        string descrip = Enum.GetName(typeof(IllinoisNonDisclosureConvictionFormOptions), answer);
        if(descrip == request.Question4)
        {
            documentModel.Sections.Add(new Section(documentModel, new Paragraph(documentModel, descrip)));
        }
    }
}

This post helped a lot! Get Enum from Description attribute

Community
  • 1
  • 1
Lostaunaum
  • 697
  • 1
  • 10
  • 31