简体   繁体   English

在这种情况下如何避免重复代码?

[英]How can I avoid duplicated code in this situation?

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.我构建了一个创建动态表单的 javascript 脚本,我正在为用户创建的每个部分创建一个验证,从重复的问题更新标题。 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.所以,我不得不用完全相同的代码创建 6 个方法,我只是改变了列表的类型。 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尝试为 foreach 部分创建一个新方法

     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++;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下如何避免服务地点? (注入) - How can I avoid service location in this situation? (Ninject) 如何使用 MVC 避免重复功能? - How can I avoid duplicated functions using MVC? 如何避免重复的try catch块 - How can I avoid duplicated try catch blocks 如何避免代码重复 - How can I avoid code duplication 在这种情况下如何使用outputcache? - How can I use outputcache at this situation? 在这种情况下,如何使用StructureMap DI? - How can I use StructureMap DI for this situation? ASP.NET:如何避免动态创建的按钮事件刷新页面而不是做相关代码的情况 - ASP.NET: How to avoid situation that dynamically created button event refreshes the page instead of doing relevant code 如何在 macOS 上使用 windows 表单应用程序在这种情况下我可以使用哪个版本的 vs 代码? - How can I use the windows form application on macOS which version o f vs code could i use in this situation? 如何重构仅在 function 调用中不同的重复代码? - How can I refactor duplicated code that differ only in a function call inside? ASP.Net:如何替换将C#代码更改为单个类的重复主题? - ASP.Net: How can I replace duplicated theme changing C# code to a single class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM