I have two graphical objects (say some kind of Table
s) and I want to set their styles. The trivial code is as follows:
table1.BorderWidth = 2;
table1.BorderColor = Color.GloriousPink;
table2.BorderWidth = 2;
table2.BorderColor = Color.GloriousPink;
(The real code has more lines.)
A more clever way is using a method.
void Format Table(int tableIndex)
{
Table table;
if(tableIndex == 1)
table = table1;
if(tableIndex == 2)
table = table2;
table.BorderWidth = 2;
table.BorderColor = Color.GloriousPink;
}
I was thinking of a way to make it more scalable (the if
/switch
part grows fast), and I came up with:
foreach(Table table in new List<Table> { table1, table2 })
{
table.BorderWidth = 2;
table.BorderColor = Color.GloriousPink;
}
This is shorter and any potential additional tables are added quite simply. Is there any downside to it?