fedeteka fedeteka - 4 months ago 16
Vb.net Question

iTextSharp Table: do I need to DIM any cell or column or can I reuse them?

I have a table of 3 columns

This code works as I make a variable asignation for any cell before the Dim

text1 = field1
Dim cell1 As New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell1)

text2 = field2
Dim cell2 As New PdfPCell(New Phrase(text2, myFont))
table.AddCell(cell2)

text3 = field3
Dim cell3 As New PdfPCell(New Phrase(text3, myFont))
table.AddCell(cell3)


but why a code like this one didn't work?

text1 = field1
Dim cell1 As New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell1)

text1 = field2
table.AddCell(cell2)

text1 = field3
table.AddCell(cell3)


I really need to define or DIM any cell or column one by one?

Answer

You need a new instance per cell but you do not have to create a new variable. You can reuse the variable name. Just assign a new instance when needed.

Dim cell As PdfPCell

text1 = field1
cell = New PdfPCell(New Phrase(text1, myFont))
table.AddCell(cell)

text2 = field2
cell = New PdfPCell(New Phrase(text2, myFont))
table.AddCell(cell)

text3 = field3
cell = New PdfPCell(New Phrase(text3, myFont))
table.AddCell(cell)