Reputation: 705
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.
Upvotes: 0
Views: 2275
Reputation: 20630
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.
Upvotes: 1
Reputation: 705
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
Upvotes: 0
Reputation: 1786
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.
Upvotes: 1