I built a javascript script that creates dynamic forms, and I'm creating a validation for each section that the user creates, that updates the title from the duplicated questions. So, I built a method for that. The user can choose between creating a section with dropboxes, checkboxes...
So, I had to create 6 methods with exacly the same code, I'm only changing the type of the list. Is there any wait to prevent that, creating a generic method?
private static int CheckDuplicatedDropdownQuestions(Template template, int i, List<string> collectQuestions)
{
foreach (var field in template.Fields.Dropdown)
{
if (!collectQuestions.Contains(field.Title))
{
collectQuestions.Add(field.Title);
}
else
{
field.Title = field.Title + " (" + i + ")";
i++;
}
}
return i;
}
...
Try creating a new method for the foreach part
private static int CheckDuplicatedDropdownQuestions(Template template, int i,
List<string> collectQuestions,string field)
{
if(field=="Dropdown")
{
NewMethod(template.Fields.Dropdown,ref i);
}
else if(field=="CheckBox")
{
NewMethod(template.Fields.CheckBox,ref i);
}
return i;
}
private static void NewMethod(var FieldType,ref int i)
{
foreach (var field in FieldType)
{
if (!collectQuestions.Contains(field.Title))
{
collectQuestions.Add(field.Title);
}
else
{
field.Title = field.Title + " (" + i + ")";
i++;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.