DictionaryBase: enumeration out of order

G

Guest

I implemented a collection inheriting from DictionaryBase.
I noticed that, adding instances via Add method, when I enumerate the
elements stored they are returned in a different order compared to input.
I attach an example like that published on MSDN.
Why this strange behaviour?
How can I to obtain the elements in the same sequence in which they are
inserted?

Ciao and thank you.
Nicola

===========================================

Imports System
Imports System.Collections

Public Class ShortStringDictionary
Inherits DictionaryBase

Default Public Property Item(ByVal key As String) As String
Get
Return CType(Dictionary(key), String)
End Get
Set(ByVal Value As String)
Dictionary(key) = Value
End Set
End Property

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Add(ByVal key As String, ByVal value As String)
Dictionary.Add(key, value)
End Sub 'Add

Public Function Contains(ByVal key As String) As Boolean
Return Dictionary.Contains(key)
End Function 'Contains

Public Sub Remove(ByVal key As String)
Dictionary.Remove(key)
End Sub 'Remove

End Class 'ShortStringDictionary

Public Class SamplesDictionaryBase

Public Shared Sub Main()

' Creates and initializes a new DictionaryBase.
Dim mySSC As New ShortStringDictionary

' Adds elements to the collection.
Console.WriteLine("During insert:")
Console.WriteLine("One, uno") : mySSC.Add("One", "uno")
Console.WriteLine("Two, due") : mySSC.Add("Two", "due")
Console.WriteLine("Three, tre") : mySSC.Add("Three", "tre")
Console.WriteLine("Four, quattro") : mySSC.Add("Four", "quattro")
Console.WriteLine("Five, cinque") : mySSC.Add("Five", "cinque")

Console.WriteLine()

' Displays the contents of the collection using the enumerator.
Console.WriteLine("Initial contents of the collection
(PrintKeysAndValues):")
PrintKeysAndValues(mySSC)

End Sub 'Main

' Uses the enumerator.
Public Shared Sub PrintKeysAndValues(ByVal myCol As ShortStringDictionary)
Dim myDE As DictionaryEntry
Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
If Not (myEnumerator.Current Is Nothing) Then
myDE = CType(myEnumerator.Current, DictionaryEntry)
Console.WriteLine(" {0,-5} : {1}", myDE.Key, myDE.Value)
End If
End While
Console.WriteLine()
End Sub 'PrintKeysAndValues

' Uses the foreach statement which hides the complexity of the enumerator.
Public Shared Sub PrintKeysAndValues2(ByVal myCol As ShortStringDictionary)
Dim myDE As DictionaryEntry
For Each myDE In myCol
Console.WriteLine(" {0,-5} : {1}", myDE.Key, myDE.Value)
Next myDE
Console.WriteLine()
End Sub 'PrintKeysAndValues2

' Uses the Keys property and the Item property.
Public Shared Sub PrintKeysAndValues3(ByVal myCol As ShortStringDictionary)
Dim myKeys As ICollection = myCol.Keys
Dim k As [String]
For Each k In myKeys
Console.WriteLine(" {0,-5} : {1}", k, myCol(k))
Next k
Console.WriteLine()
End Sub 'PrintKeysAndValues3

End Class 'SamplesDictionaryBase

===========================================
 
J

Jon Skeet [C# MVP]

Nick said:
I implemented a collection inheriting from DictionaryBase.
I noticed that, adding instances via Add method, when I enumerate the
elements stored they are returned in a different order compared to input.
I attach an example like that published on MSDN.
Why this strange behaviour?

Because you've got a dictionary, not a list. A dictionary doesn't
attempt to keep track of insertion order - that's not part of its job.
Instead, it keeps the entries in whatever order it deems most efficient
- and that order can change over its lifetime (although I'd be somewhat
surprised if it changed without the contents changing).
How can I to obtain the elements in the same sequence in which they are
inserted?

Keep a list of keys in insertion order separately.
 

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