By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,375 Members | 3,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,375 IT Pros & Developers. It's quick & easy.

Confused by sorted list of a sorted list

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
On Sat, 25 Jun 2005 16:21:02 -0700, J L <jo**@marymonte.com> wrote:
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


Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.