1
 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. 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?

4 Answers 4

2

One approach to simplifying this is to use an Action callback for each specific case:

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);
....
Sign up to request clarification or add additional context in comments.

Comments

0

Use the Controls collection of the form object: (typecast)Controls(of+"PAN1").SomeProperty = some value;

1 Comment

Any examples or can you change my code as per that systax.
0

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

Comments

-1

In your case, you can do only one foreach and move the test inside of the loop.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.