Hi,
Seeing as I'm relitively new to c#, and its Friday, I thought I'd post this little code conundrum here, even though its not related to LLBLGen:
Look at this code:
if (colName.ToLower().EndsWith("_image"))
{
// This column is an image.
TemplateColumn bc = new TemplateColumn();
bc.ItemTemplate = new DataGridImageTemplate(ListItemType.Item, colName);
bc.HeaderTemplate = new DataGridImageTemplate(ListItemType.Header, colName);
bc.SortExpression = "zzz";
if (dc.widthPercent > 0)
{
System.Web.UI.WebControls.Unit unit = new Unit(Convert.ToString(dc.widthPercent) + "%");
bc.ItemStyle.Width = unit;
}
DataGrid1.Columns.Add(bc);
}
else
{
BoundColumn bc = new BoundColumn();
bc.HeaderText = colName;
bc.DataField = colName;
bc.SortExpression = colName;
if (dc.widthPercent > 0)
{
System.Web.UI.WebControls.Unit unit = new Unit(Convert.ToString(dc.widthPercent) + "%");
bc.ItemStyle.Width = unit;
}
DataGrid1.Columns.Add(bc);
}
}
Basically if some condition is true I want to create an "X" object, else create a "Y" object. Both objects inherit from the same base, and I perform similar code on both (the width setting), and set some properties depending on the action.
But this code isn't great. It reperats half the logic code! So, how do I change it so that the "width" code is only defined once? There is no interface these objects implement, they just share a common base......
Would appreciate any feedback!!