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?