简体   繁体   English

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

[英]How to reduce repeated code in foreach loop

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

I have a foreach loop like above and i'm repeating the same code inorder to pass each panel value. 我有一个如上所述的foreach循环,我在重复相同的代码以传递每个面板值。 I'm trying to reduce the repeated code like below( but not sure it is correct way ) 我正在尝试减少重复的代码,如下所示(但不确定这是正确的方法)

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

Is there a better way to write this above foreach logic with minimal code? 是否有更好的方法以最少的代码在foreach逻辑上编写此代码?

One approach to simplifying this is to use an Action callback for each specific case: 一种简化方法是针对每种特定情况使用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);
....

使用表单对象的Controls集合:(typecast)Controls(of +“ PAN1”)。SomeProperty =一些值;

I just think foreach-ing the collection three times is a little wasteful. 我只是认为对收藏进行三遍是有点浪费。 Maybe something like this might be a little more performant 也许像这样的东西可能会表现得更好

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

In your case, you can do only one foreach and move the test inside of the loop. 在您的情况下,您只能进行一次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;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM