better way to program than this?

  • Thread starter Thread starter André
  • Start date Start date
A

André

Hi,

I was wondering whether there is a better way to program code than this:
assume i need to create a lot of TabelCell with the same characterists
(HorizontalAlign, BackColor etc ...).

I can't use a loop (for/next) because the 'Text' is always different. What i
did was:

Dim c as TableCell
Dim r as TableRow
....


'first cell
c = New TableCell
c.HorizontalAlign = HorizontalAlign.Center
c.BackColor = Drawing.Color.LightSteelBlue
c.Text=a
r.Cells.Add(c) ' add to row

'next cell
c = New TableCell
c.HorizontalAlign = HorizontalAlign.Center
c.BackColor = Drawing.Color.LightSteelBlue
c.Text=b
r.Cells.Add(c) ' add to row


etc ...


Is it not possible to define once all the characteristics of c instead of
repeating them for each cell?

Thanks
André
 
Andre:

Don't fail to take advantage of simple structured solutions. While you
could create a subclass (I wouldn't in this case) the easy solution is to
add a private function that returns a New TableCell that is pre-filled in.
Pass that function the text you want and it will be assigned as well. This
can be placed in a loop with a bit more tweaking (creating an array of text
values) but you probably don't have to go that far. So you end up with:

r.CellsAdd( MyNewTableCell(a))
r.CellsAdd( MyNewTableCell(b))
etc.

Tom
 
of course. You can place them in the class constructor.
Or make a sub that assigns them.

-t :)

André ha scritto:
 
André said:
I was wondering whether there is a better way to program code than this:
assume i need to create a lot of TabelCell with the same characterists
(HorizontalAlign, BackColor etc ...).

I can't use a loop (for/next) because the 'Text' is always different.
<snip>

If the only thing that makes a cell appart from the others is the text,
you can pull the text from an array in a For Each /Next loop:

<aircode>
Dim CellText() AS String = { _
"Text1", "Text2", "Text3", ..., "TextN" _
}

For Each Text As String In CellText

Dim C as New TableCell
C.HorizontalAlign = HorizontalAlign.Center
C.BackColor = Drawing.Color.LightSteelBlue
C.Text= Text
...
... Add the cell to the row
...
Next
</aircode>

HTH.

Regards,

Branco.
 
Back
Top