0

I'm having to build a table of web pages and languages, i.e. page 1: en it de in a schema where there could be up to 14 languages. The page contents are held in a database. So to build the table I'm doing the following:

Dim rowArrayList As New ArrayList
Dim thisRow(languageNum) As String 'languageNum equates to number of columns -1

Database access then:

'# Create row array of cell arrays
If pageName <> lastPageName Then
    lastPageName = pageName
    If j >= languageNum Then
        rowArrayList.Add(thisRow)
        Array.Clear(thisRow, 0, thisRow.Length)
        j = 0
    End If
    thisRow(0) = "<td class=""pageName"">" & pageName & "</td>"
End If

'# Iterate each cell in the row
For i As Integer = 1 To languageNum - 1
    If thisRow(i) = "" Then
        If transReady = False And active = False Then
            thisRow(i) = "<td class=""trans""><a href=""content/page-text.aspx?pageID=" & SQLReader("DAEPageContentControlID").ToString() & "&lang=" & langISO & """>" & langISO & "</a></td>"
        ElseIf transReady = True And active = False Then
            thisRow(i) = "<td class=""notActive""><a href=""content/page-text.aspx?pageID=" & SQLReader("DAEPageContentControlID").ToString() & "&lang=" & langISO & """>" & langISO & "</a></td>"
        ElseIf transReady = True And active = True And i = thisLangID Then
            thisRow(i) = "<td class=""active""><a href=""content/page-text.aspx?pageID=" & SQLReader("DAEPageContentControlID").ToString() & "&lang=" & langISO & """>" & langISO & "</a></td>"
        End If
    End If
    j = j + 1
Next

The build the table:

'# Build output table
For Each row As String() In rowArrayList
    tableBody.Text += "<tr>"

    For Each cell As String In row
        If cell = "" Then
            tableBody.Text += "<td class=""notTrans"">&nbsp;</td>"
        Else
            tableBody.Text += cell
        End If
    Next

    tableBody.Text += "</tr>"
Next

The table displays beautifully BUT every row contains the data for what should be the last row. How can it be fixed it so each thisRow is unique in the the rowArrayList? At the moment, every time thisRow is added to rowArrayList, every rowArrayList index is overwritten, not just the one being added.

2
  • This is tagged ASP.NET so why don't you use ASP.NET controls like Repeater or GridView instead of creating html manually? Commented Oct 10, 2013 at 21:07
  • Don't use the ArrayList type in .Net. Ever. In this case, the obvious replacement is List(Of String()) Commented Oct 10, 2013 at 21:09

1 Answer 1

1

For the quick fix, instead of this:

Array.Clear(thisRow, 0, thisRow.Length)

Do this:

thisRow = New String(languageNum) {}

or this:

ReDim thisRow(languageNum)

However, I suspect there are some simple design choices you could change that would drastically change this code for the better.

Sign up to request clarification or add additional context in comments.

6 Comments

He needs to use a string builder. I just did something like this gimmie a minute I'll write up an answer
There probably are some design choices but if you're not aware of the choices, you can't make 'em. The "or this:" section seemed to do it for me. Thanks very much :)
@Craig good deal. Google reasons for using stringbuilder in VB.net and code-optimization techniques. Stringbuilders can drastically improve performance.
It's often faster to throw the old array or list away and recreate a new one, than it is to .Clear it. And +1 for StringBuilder class. Strings are immutable.
Thanks guys, this was just a quick refactor of some old code in a rarely used section of an admin system so performance isn't an issue. Just been reading about the C# hashset which, if it was a C# project is probably what I'd have used, though stringbuilders could have been an option. Thanks again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.