如何减少foreach循环中重复的代码

时间:2015-11-20 21:30:59

标签: c# foreach

 if (!string.IsNullOrEmpty(View.Panel1.ToString()))
            {
                foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.PAN1 = View.Panel1;
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }
                }
            }
 if (!string.IsNullOrEmpty(View.Panel2.ToString()))
            {
                foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.PAN2 = View.Panel2;
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }
                }
            }
if (!string.IsNullOrEmpty(View.Panel3.ToString()))
            {
                foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.PAN3 = View.Panel3;
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }

                }
            }
if (!string.IsNullOrEmpty(View.Panel4.ToString()))
            {
                foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.PAN4 = View.Panel4;
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }
                }
            }
if (!string.IsNullOrEmpty(View.Panel5.ToString()))
            {
                foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.PAN5 = View.Panel5;
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }
                }
            }
.....
.....

我有一个像上面这样的foreach循环,我重复相同的代码以传递每个面板值。 我试图减少重复的代码,如下所示(但不确定它是否正确)

if (!string.IsNullOrEmpty(View.Panel1.ToString()))
{
   setpanelinfo(View.Panel1.ToString(),PAN1)
}
if (!string.IsNullOrEmpty(View.Panel2.ToString()))
{
   setpanelinfo(View.Panel2.ToString(),PAN2)
}
....
....
....

public void setpanelinfo(string strpanelvalue, string PAN)
{
   foreach (OtherFeatures of in FeaturesInfo)
                {
                    if (of != null)
                    {
                        of.+ "PAN1" = strpanelvalue; // ERROR
                        of.NumOtherFeatures = null;
                        of.OtherFeaturesDesc = null;
                        break;
                    }
                }
}

有没有更好的方法用最少的代码编写上面的foreach逻辑?

4 个答案:

答案 0 :(得分:2)

简化此操作的一种方法是对每个特定情况使用Action回调:

void HandlePanel(string panel, Action<OtherFeatures> action)
{
    if (!string.IsNullOrEmpty(panel))
    {
        foreach (var of in FeaturesInfo)
        {
            if (of != null)
            {
                action(of);
                of.NumOtherFeatures = null;
                of.OtherFeaturesDesc = null;
                break;
            }
        }
    }
}

...

HandlePanel(View.Panel1.ToString(), of => of.PAN1 = View.Panel1);
HandlePanel(View.Panel2.ToString(), of => of.PAN2 = View.Panel2);
HandlePanel(View.Panel3.ToString(), of => of.PAN3 = View.Panel3);    
HandlePanel(View.Panel4.ToString(), of => of.PAN4 = View.Panel4);
....

答案 1 :(得分:0)

使用表单对象的Controls集合: (类型转换)控制(+&#34; PAN1&#34;)。SomeProperty =某个值;

答案 2 :(得分:0)

我只是认为三次收集这个系列有点浪费。也许这样的事情可能会更高效一点

foreach (var of in FeaturesInfo)
{
    if (of != null)
    {
        TestAndSet(View.Panel1.ToString(), text => of.PAN1 = text);
        TestAndSet(View.Panel2.ToString(), text => of.PAN2 = text);
        TestAndSet(View.Panel3.ToString(), text => of.PAN3 = text);
        of.NumOtherFeatures = null;
        of.OtherFeaturesDesc = null;
        break;
    }
}
....
private void TestAndSet(String panel, Action<string> setAction)
{
    if (!string.IsNullOrEmpty(panel))
    {
       setAction(panel);
    }        
}

答案 3 :(得分:-1)

在你的情况下,你只能做一个foreach并在循环中移动测试。

foreach (OtherFeatures of in FeaturesInfo)
{
    if (of != null)
    {
        of.NumOtherFeatures = null;
        of.OtherFeaturesDesc = null;

        if (!string.IsNullOrEmpty(View.Panel1.ToString()))
            of.PAN1 = View.Panel1;
        if (!string.IsNullOrEmpty(View.Panel2.ToString()))
            of.PAN2 = View.Panel2;
        if (!string.IsNullOrEmpty(View.Panel3.ToString()))
            of.PAN3 = View.Panel3;
        if (!string.IsNullOrEmpty(View.Panel4.ToString()))
            of.PAN4 = View.Panel4;
        if (!string.IsNullOrEmpty(View.Panel5.ToString()))
            of.PAN5 = View.Panel5;

        break;
    }
}