Confused by sorted list of a sorted list

J

J L

I want to create a sorted list whose values are themselves sorted
lists. I wrote the following simple test program but it does not
behave as I would expect.

What I wanted to do was have the doorConflictList be keyed on a door
ID and contain a sorted list called conFlictList. I wrote this
expecting the following

doorConflictList should end up with 3 items.
Item 1 would be a sorted list with 1 item
Item 2 would be a sorted list with 2 items
Item 3 would be a sorted list with 3 items.

I expected to see :

Door 0
Value 0
Door 1
Value 0
Value 1
Door 2
Value 0
Value 1
Value 2

but instead I see:

Door 0
Value 0
Value 1
Value 2
Door 1
Value 0
Value 1
Value 2
Door 2
Value 0
Value 1
Value 2

What am I doing wrong?

Here is the code :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim conFlictList As New SortedList
Dim doorConflictList As New SortedList
Dim i As Integer
Dim m As Integer
Dim door As Integer
Dim numberOfDoors As Integer = 3
For door = 0 To numberOfDoors - 1
conFlictList.Clear()
For i = 0 To door
conFlictList.Add("Key " & i.ToString, "Value " &
i.ToString)
Next
doorConflictList.Add("Door " & door.ToString, conFlictList)
Next
Dim strMessage As String = ""
If doorConflictList.Count > 0 Then
For i = 0 To doorConflictList.Count - 1
strMessage += doorConflictList.GetKey(i) & vbCrLf
Dim theConflicts As SortedList = _
CType(doorConflictList.GetByIndex(i), SortedList)
If theConflicts.Count > 0 Then
For m = 0 To theConflicts.Count - 1
strMessage += " " & theConflicts.GetByIndex(m) &
vbCrLf
Next
End If
Next
End If
MessageBox.Show(strMessage)
End Sub

TIA
John
 
J

J L

Opps...embarrasing...I see it now...scope issue...
Needed to move the
Dim conflictlist as New SortedList
into the loop creating it, else I was pointing to the same sorted
list every time and hence the last one filled had the 3 values...well
that was not well stated but I see the problem...

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top