Friday conundrum....

Posts   
 
    
Posts: 497
Joined: 08-Apr-2004
# Posted on: 20-Aug-2004 16:49:25   

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!!

netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 20-Aug-2004 17:01:58   

something like this...?

DataGridColumn bc = null;

if (colName.ToLower().EndsWith("_image"))
{
    // This column is an image.
    bc = new TemplateColumn();
    bc.ItemTemplate = new DataGridImageTemplate(ListItemType.Item, colName);
    bc.HeaderTemplate = new DataGridImageTemplate(ListItemType.Header, colName);
    bc.SortExpression = "zzz";
}
else
{
    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);

Posts: 497
Joined: 08-Apr-2004
# Posted on: 21-Aug-2004 22:01:19   

Thanks, this is something that I tried, but I got compile errors because a TemplateColumn has properties I am setting that a DataGridColumn doesn't. I can only get at the properties of "bc" which are properties of a datagridcolumn, even though I instantiate "bc" as a new TemplateColumn. I thought I would be able to do this, but apparently not!

netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 23-Aug-2004 11:04:07   

Ok, you'd have to cast it to the correct type then, e.g.


DataGridColumn bc = null;

if (colName.ToLower().EndsWith("_image"))
{
    // This column is an image.
    bc = new TemplateColumn();
    ((TemplateColumn)bc).ItemTemplate = new DataGridImageTemplate(ListItemType.Item, colName);
    ((TemplateColumn)bc).HeaderTemplate = new DataGridImageTemplate(ListItemType.Header, colName);
    ((TemplateColumn)bc).SortExpression = "zzz";
}
else
{
    bc = new BoundColumn();
    ((BoundColumn)bc).HeaderText = colName;
    ((BoundColumn)bc).DataField = colName;
    ((BoundColumn)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);

Posts: 497
Joined: 08-Apr-2004
# Posted on: 23-Aug-2004 16:23:53   

Ah, that seems to do the trick.

Thanks for the response simple_smile