简化代码

时间:2011-09-01 12:41:25

标签: asp.net .net-4.0

 private const string CurrentPageCssClass = "important";
        private void HidhLightCurrentPageIndex() {

            switch (tpMain.ActiveTabIndex) {
                case 0:
                    elblFirst.AddClass(CurrentPageCssClass);
                    elblSecond.RemoveClass(CurrentPageCssClass);
                    elblThird.RemoveClass(CurrentPageCssClass);
                    elblFouth.RemoveClass(CurrentPageCssClass);
                    elblFirth.RemoveClass(CurrentPageCssClass);
                    break;
                case 1:
                    elblFirst.RemoveClass(CurrentPageCssClass);
                    elblSecond.AddClass(CurrentPageCssClass);
                    elblThird.RemoveClass(CurrentPageCssClass);
                    elblFouth.RemoveClass(CurrentPageCssClass);
                    elblFirth.RemoveClass(CurrentPageCssClass);

                    break;
                case 2:
                    elblFirst.RemoveClass(CurrentPageCssClass);
                    elblSecond.RemoveClass(CurrentPageCssClass);
                    elblThird.AddClass(CurrentPageCssClass);
                    elblFouth.RemoveClass(CurrentPageCssClass);
                    elblFirth.RemoveClass(CurrentPageCssClass);


                    break;
                case 3:
                    elblFirst.RemoveClass(CurrentPageCssClass);
                    elblSecond.RemoveClass(CurrentPageCssClass);
                    elblThird.RemoveClass(CurrentPageCssClass);
                    elblFouth.AddClass(CurrentPageCssClass);
                    elblFirth.RemoveClass(CurrentPageCssClass);
                    break;
                case 4:
                    elblFirst.RemoveClass(CurrentPageCssClass);
                    elblSecond.RemoveClass(CurrentPageCssClass);
                    elblThird.RemoveClass(CurrentPageCssClass);
                    elblFouth.RemoveClass(CurrentPageCssClass);
                    elblFirth.AddClass(CurrentPageCssClass);
                    break;

            }
        }

有人对如何简化此代码有任何建议吗?

3 个答案:

答案 0 :(得分:4)

 private const string CurrentPageCssClass = "important";
        private void HidhLightCurrentPageIndex() {

            elblFirst.RemoveClass(CurrentPageCssClass);
            elblSecond.RemoveClass(CurrentPageCssClass);
            elblThird.RemoveClass(CurrentPageCssClass);
            elblFouth.RemoveClass(CurrentPageCssClass);
            elblFirth.RemoveClass(CurrentPageCssClass);
            switch (tpMain.ActiveTabIndex) {
                case 0:
                    elblFirst.AddClass(CurrentPageCssClass);
                    break;
                case 1:
                    elblSecond.AddClass(CurrentPageCssClass);    
                    break;
                case 2:
                    elblThird.AddClass(CurrentPageCssClass);
                    break;
                case 3:
                    elblFouth.AddClass(CurrentPageCssClass);
                    break;
                case 4:
                    elblFirth.AddClass(CurrentPageCssClass);
                    break;    
            }
        }

答案 1 :(得分:3)

将控件放在一个数组中,然后在所有元素上调用RemoveClass,然后在活动元素上调用AddClass。'

编辑:代码

var controls = new[] { elblFirst, elblSecond, elblThird, elblFourth, elblFifth };
foreach (var control in controls)
{
    control.RemoveClass(CurrentPageCssClass);
}

controls[tpMain.ActiveTabIndex].AddClass(CurrentPageCssClass);

当然这是假设对RemoveClass的额外调用具有可忽略的成本,否则使用常规for循环并跳过活动索引。

答案 2 :(得分:2)

这是另一个减少代码重复并更多地遵守DRY原则的选项:

private const string CurrentPageCssClass = "important";

private void toggleClass(object elbl, bool addClass) {
    if (addClass) {
        elbl.AddClass(CurrentPageCssClass);
    } else {
        elbl.RemoveClass(CurrentPageCssClass);
    }
}

private void HidhLightCurrentPageIndex() {
    int index = tpMain.ActiveTabIndex;
    toggleClass(elblFirst, index == 0);
    toggleClass(elblSecond, index == 1);
    toggleClass(elblThird, index == 2);
    toggleClass(elblFouth, index == 3);
    toggleClass(elblFirth, index == 4);
}

其他帖子建议在所有项目上调用“RemoveClass”,然后在您想要的项目上调用“AddClass”。但是,根据“RemoveClass”方法的不同,可能会出现这样做的性能影响。我发布的解决方案只会调用适当的方法,而不需要任何额外的,不必要的方法调用,而且它要短得多。