slister
slister

Reputation: 789

How do I make a list of list objects In VB.NET?

Hi I know this may sound a little weird but I am trying to have a master list that contains other lists of controls. Here is what I have so far.

'Create master list of control lists. Each member of this list will be a list containing one rows worth of controls

Dim masterList As New List(Of List(Of Control))
Dim rowList As New List(Of Control)

For Each Control As Control In flpExceptionControls.Controls

    rowList.Add(Control)

    If flpExceptionControls.GetFlowBreak(Control) = True Then
        masterList.Add(rowList)
        rowList.Clear()
    End If

Next

For Each row As List(Of Control) In masterList

    MsgBox(row.Count.ToString)

Next

The message box is showing that each of those lists have a count of 0, but I know it is adding all the controls to those objects because it shows it when I step through the code. I'm guessing I am just not accessing the list objects contained in the master list correctly.

Any suggestions would be greatly appreciated.

Upvotes: 2

Views: 12671

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40150

Your problem is here:

If flpExceptionControls.GetFlowBreak(Control) = True Then
   masterList.Add(rowList)
   rowList.Clear()
End If

You are clearing the contents of the same list, which you are then adding some new items to, and clearing again, and adding the same reference to the master list. Essentially; all of your items in masterList are the same, empty list.

You need to create a new sub-list for each. Don't clear any lists.

Dim rowList As New List(Of Control)
For Each Control As Control In flpExceptionControls.Controls
    rowList.Add(Control)
    If flpExceptionControls.GetFlowBreak(Control) = True Then
        masterList.Add(rowList)
        rowList = New List(Of Control)
    End If
Next

Upvotes: 2

Related Questions