Home All Groups Group Topic Archive Search About

better way to program than this?

Author
27 Nov 2006 6:33 PM
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é

Author
27 Nov 2006 10:22 PM
Tom Leylan
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


Show quoteHide quote
"André" <hjhhb@dd> wrote in message
news:eccIYKlEHHA.4464@TK2MSFTNGP06.phx.gbl...
> 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é
>
Author
27 Nov 2006 10:28 PM
tommaso.gastaldi@uniroma1.it
of course. You can place them in the class constructor.
Or make a sub that assigns them.

-t :)

André ha scritto:

Show quoteHide quote
> 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é
Author
28 Nov 2006 9:06 AM
André
Thanks both

<tommaso.gasta***@uniroma1.it> schreef in bericht
news:1164666482.507686.96720@f16g2000cwb.googlegroups.com...
of course. You can place them in the class constructor.
Or make a sub that assigns them.

-t :)

André ha scritto:

Show quoteHide quote
> 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é
Author
28 Nov 2006 2:37 PM
Branco Medeiros
André wrote:
> 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.