PC Review


Reply
Thread Tools Rate Thread

Confused by sorted list of a sorted list

 
 
J L
Guest
Posts: n/a
 
      26th Jun 2005
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
 
Reply With Quote
 
 
 
 
J L
Guest
Posts: n/a
 
      26th Jun 2005
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 <(E-Mail Removed)> 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


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorted List by value =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= Microsoft C# .NET 12 6th Feb 2007 09:59 PM
How to get a KEY from a sorted list (not a VALUE) ?? pamelafluente@libero.it Microsoft VB .NET 1 18th Sep 2006 09:03 PM
Sorted list G Chartrand Microsoft Excel Misc 2 28th Apr 2006 05:07 PM
last row in sorted list =?Utf-8?B?VHhSYWlzdGxpbg==?= Microsoft Excel Programming 1 29th Mar 2005 11:19 PM
Sorted Key,Value list Leif Eirik Olsen Microsoft Dot NET Compact Framework 6 15th Mar 2005 07:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:13 AM.