|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
better way to program than this?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 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é > 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é 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é André wrote:
> I was wondering whether there is a better way to program code than this: <snip>> 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. 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.
Re: Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested
listbox's SelectedIndexChanged keep fire Array Question, Out of Bounds how to save the textbox contents to sql server database Newbie Display Outlook Recipient Window from vb.net Errors when running from LAN server. using file system in web site Setting Time property Form level focus monitor |
|||||||||||||||||||||||