Question Iterating Through DictionaryEntries Adding Keys and Values to a Table

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
I have a little project, where I want to display various pieces of information on web page. Part of the information is the capabilites of the user's browser.
So far, the following code has grabbed all of the capabilities and displayed then onscreen, albeit in a crude form
Code:
        Dim bc As HttpBrowserCapabilities = Request.Browser

        For Each de As DictionaryEntry In bc.Capabilities
             Response.Write(de.Key & " = " & de.Value & "<br>")
        Next
What I would like to do, is display the information in a table. As the information is fairly short, I think I could display two key/value pairs per row. That is to say, I want my table to have four columns per row, the first will hold the key of the first dictionaryEntry, the second will hold the value. The third column will hold the key of the next dictionaryEntry and the fourth will hold the value. The next row will follow the same format until all of the key/value pairs are displayed.

I'm struggling with the logic to get the results I want. I need the get two dictionaryEntries at a time in order to create and assign values to my four columns which form a row, add the row and then do the same for the next two dictionaryEntries.

I'd appreciate any suggestions.

Jason
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,411
Location
Norway
Programming Experience
10+
how about using a Boolean variable?
        Dim first = True
        For Each de As DictionaryEntry In Request.Browser.Capabilities
            If first Then
                'start row
                'add item cells
            Else
                'add item cells
                'end row
            End If
            first = Not first
        Next

        If Not first Then 'end last row

Other than that, I recommend using Table/TableRow/TableCell rather than composing and writing strings to Response object.
 

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
Thanks for your suggestion John.
In a moment of clarity I figured out a way to do this. It's similar in principle to your suggestion, but I used an arraylist and tested to see if it contained two elements. If it did, I sent the two dictionary Entry objects to another another routine, which created and added the row to my table.
I've posted the code below, in the event that it helps someone else with a similar issue.

Code:
Dim bc As HttpBrowserCapabilities = Request.Browser

        Dim alEntries As ArrayList = New ArrayList ' Cretae an arraylist to hold dictionaryEntries
        For Each de As DictionaryEntry In bc.Capabilities
            alEntries.Add(de)
            If alEntries.Count Mod 2 = 0 Then ' Test that arraylist has two elements
                addBrowserRow(alEntries)      ' Call routine to create row
                alEntries.Clear()             ' Clear the arraylist
            End If
        Next

        'Test to see if the arraylist still has an element (odd number of dictioanry entries at the end of the iteration)
        If alEntries.Count = 1 Then
            addBrowserRow(alEntries) ' Call routine to add row with only one value
        End If


Private Sub addBrowserRow(ar As ArrayList)
        Dim tCell1 As New TableCell
        Dim tCell2 As New TableCell
        Dim tCell3 As New TableCell
        Dim tCell4 As New TableCell
        tCell1.Font.Bold = True
        tCell3.Font.Bold = True
        tCell1.HorizontalAlign = HorizontalAlign.Right
        tCell3.HorizontalAlign = HorizontalAlign.Right

        If ar.Count = 1 Then
            tCell1.Text = ar(0).key
            tCell2.Text = ar(0).value
            tCell3.Text = ""
            tCell4.Text = ""
        ElseIf ar.Count = 2 Then
            tCell1.Text = ar(0).key
            tCell2.Text = ar(0).value
            tCell3.Text = ar(1).key
            tCell4.Text = ar(1).value
        End If

        Dim tRow As New TableRow
        tRow.Cells.Add(tCell1)
        tRow.Cells.Add(tCell2)
        tRow.Cells.Add(tCell3)
        tRow.Cells.Add(tCell4)
        tbleBrowser.Rows.Add(tRow)

    End Sub
 
Top Bottom