DictionaryBase ordering

M

marc

I want to create a strongly typed collection and use DictionaryBase

I add a number of objects but when i loop through the list the data is
ordered in a strange maner. How is this possible?

Can anybody tell my why the order gets changed?

Thanks Marc

'***************** OUTPUT **************************

employee Zack has internal number 8501
employee Robert has internal number 8502
employee John has internal number 8503
employee Alec has internal number 8504
employee Carl has internal number 8505
employee Theo has internal number 8500



'***************** CODE **************************

Module Module1

Sub Main()
Dim myEmployees As New cPersons
myEmployees.Add(New cPerson("Theo", "8500"))
myEmployees.Add(New cPerson("Zack", "8501"))
myEmployees.Add(New cPerson("Robert", "8502"))
myEmployees.Add(New cPerson("John", "8503"))
myEmployees.Add(New cPerson("Alec", "8504"))
myEmployees.Add(New cPerson("Carl", "8505"))

Try
For Each employee As cPerson In myEmployees.List
Console.WriteLine("employee {0} has internal number {1}",
employee.Name, employee.InternalPhoneNumber)
Next

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module

Public Class cPerson

Dim _key, _name, _internalPhone As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property InternalPhoneNumber() As String
Get
Return _internalPhone
End Get
Set(ByVal Value As String)
_internalPhone = Value
End Set
End Property

Public Sub New(ByVal name As String, ByVal adres As String)
_name = name
_internalPhone = adres
_key = name
End Sub

Friend Property key() As String
Get
Return _key
End Get
Set(ByVal Value As String)
_key = Value
End Set
End Property
End Class
Public Class cPersons
Inherits DictionaryBase

Public Function Add(ByVal person As cPerson) As Boolean
If Not dictionary.Contains(person.key) Then
dictionary.Add(CType(person.key, String), person)
Debug.WriteLine("object added: " & person.key)
Add = True
End If
End Function

Public Function RemoveAll() As Boolean
dictionary.Clear()
Return True
End Function
Public Function Remove(ByVal key As String) As Boolean
If dictionary.Contains(key) Then
dictionary.Remove(key)
Debug.WriteLine("object removed: " & key)
Return True
End If
End Function

Public Function Remove(ByVal person As cPerson) As Boolean
If dictionary.Contains(person.key) Then
dictionary.Remove(person.key)
Debug.WriteLine("object removed: " & person.key)
Return True
End If
End Function

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

Default Public ReadOnly Property Item(ByVal key As [String]) As cPerson
Get
Return CType(Dictionary(key), cPerson)
End Get
End Property

Public ReadOnly Property List() As System.Collections.ICollection
Get
Return InnerHashtable.Values
End Get
End Property

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
R

Russell Jones

Dictionary objects (and Hashtables) don't guarantee a specific object
ordering. The documentation states that items are organized "based on the
hash code of the key. Subsequent lookups of the key use the hash code of the
key to search in only one particular bucket, thus substantially reducing the
number of key comparisons required to find an element."

If you need the specific object ordering, consider using the SortedList
class instead as your base class instead, which also accepts key-value
pairs.


marc said:
I want to create a strongly typed collection and use DictionaryBase

I add a number of objects but when i loop through the list the data is
ordered in a strange maner. How is this possible?

Can anybody tell my why the order gets changed?

Thanks Marc

'***************** OUTPUT **************************

employee Zack has internal number 8501
employee Robert has internal number 8502
employee John has internal number 8503
employee Alec has internal number 8504
employee Carl has internal number 8505
employee Theo has internal number 8500



'***************** CODE **************************

Module Module1

Sub Main()
Dim myEmployees As New cPersons
myEmployees.Add(New cPerson("Theo", "8500"))
myEmployees.Add(New cPerson("Zack", "8501"))
myEmployees.Add(New cPerson("Robert", "8502"))
myEmployees.Add(New cPerson("John", "8503"))
myEmployees.Add(New cPerson("Alec", "8504"))
myEmployees.Add(New cPerson("Carl", "8505"))

Try
For Each employee As cPerson In myEmployees.List
Console.WriteLine("employee {0} has internal number {1}",
employee.Name, employee.InternalPhoneNumber)
Next

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module

Public Class cPerson

Dim _key, _name, _internalPhone As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property InternalPhoneNumber() As String
Get
Return _internalPhone
End Get
Set(ByVal Value As String)
_internalPhone = Value
End Set
End Property

Public Sub New(ByVal name As String, ByVal adres As String)
_name = name
_internalPhone = adres
_key = name
End Sub

Friend Property key() As String
Get
Return _key
End Get
Set(ByVal Value As String)
_key = Value
End Set
End Property
End Class
Public Class cPersons
Inherits DictionaryBase

Public Function Add(ByVal person As cPerson) As Boolean
If Not dictionary.Contains(person.key) Then
dictionary.Add(CType(person.key, String), person)
Debug.WriteLine("object added: " & person.key)
Add = True
End If
End Function

Public Function RemoveAll() As Boolean
dictionary.Clear()
Return True
End Function
Public Function Remove(ByVal key As String) As Boolean
If dictionary.Contains(key) Then
dictionary.Remove(key)
Debug.WriteLine("object removed: " & key)
Return True
End If
End Function

Public Function Remove(ByVal person As cPerson) As Boolean
If dictionary.Contains(person.key) Then
dictionary.Remove(person.key)
Debug.WriteLine("object removed: " & person.key)
Return True
End If
End Function

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

Default Public ReadOnly Property Item(ByVal key As [String]) As cPerson
Get
Return CType(Dictionary(key), cPerson)
End Get
End Property

Public ReadOnly Property List() As System.Collections.ICollection
Get
Return InnerHashtable.Values
End Get
End Property

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
M

Mike McIntyre [MVP]

Check out the PrintKeysAndValues procedure at the web page below:

http://msdn.microsoft.com/library/d...systemcollectionsdictionarybaseclasstopic.asp

It shows how to access the data in sorted order.


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com


marc said:
I want to create a strongly typed collection and use DictionaryBase

I add a number of objects but when i loop through the list the data is
ordered in a strange maner. How is this possible?

Can anybody tell my why the order gets changed?

Thanks Marc

'***************** OUTPUT **************************

employee Zack has internal number 8501
employee Robert has internal number 8502
employee John has internal number 8503
employee Alec has internal number 8504
employee Carl has internal number 8505
employee Theo has internal number 8500



'***************** CODE **************************

Module Module1

Sub Main()
Dim myEmployees As New cPersons
myEmployees.Add(New cPerson("Theo", "8500"))
myEmployees.Add(New cPerson("Zack", "8501"))
myEmployees.Add(New cPerson("Robert", "8502"))
myEmployees.Add(New cPerson("John", "8503"))
myEmployees.Add(New cPerson("Alec", "8504"))
myEmployees.Add(New cPerson("Carl", "8505"))

Try
For Each employee As cPerson In myEmployees.List
Console.WriteLine("employee {0} has internal number {1}",
employee.Name, employee.InternalPhoneNumber)
Next

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module

Public Class cPerson

Dim _key, _name, _internalPhone As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property InternalPhoneNumber() As String
Get
Return _internalPhone
End Get
Set(ByVal Value As String)
_internalPhone = Value
End Set
End Property

Public Sub New(ByVal name As String, ByVal adres As String)
_name = name
_internalPhone = adres
_key = name
End Sub

Friend Property key() As String
Get
Return _key
End Get
Set(ByVal Value As String)
_key = Value
End Set
End Property
End Class
Public Class cPersons
Inherits DictionaryBase

Public Function Add(ByVal person As cPerson) As Boolean
If Not dictionary.Contains(person.key) Then
dictionary.Add(CType(person.key, String), person)
Debug.WriteLine("object added: " & person.key)
Add = True
End If
End Function

Public Function RemoveAll() As Boolean
dictionary.Clear()
Return True
End Function
Public Function Remove(ByVal key As String) As Boolean
If dictionary.Contains(key) Then
dictionary.Remove(key)
Debug.WriteLine("object removed: " & key)
Return True
End If
End Function

Public Function Remove(ByVal person As cPerson) As Boolean
If dictionary.Contains(person.key) Then
dictionary.Remove(person.key)
Debug.WriteLine("object removed: " & person.key)
Return True
End If
End Function

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

Default Public ReadOnly Property Item(ByVal key As [String]) As cPerson
Get
Return CType(Dictionary(key), cPerson)
End Get
End Property

Public ReadOnly Property List() As System.Collections.ICollection
Get
Return InnerHashtable.Values
End Get
End Property

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
M

marc

The sortedlist orders my entries alphabetically,

but I would like to keep the items ordered like added to collection

so addin object like
myEmployees.Add(New cPerson("Theo", "8500"))
myEmployees.Add(New cPerson("Zack", "8501"))
myEmployees.Add(New cPerson("Robert", "8502"))
myEmployees.Add(New cPerson("John", "8503"))
myEmployees.Add(New cPerson("Alec", "8504"))
myEmployees.Add(New cPerson("Carl", "8505"))

So when I loop through the list I would like to see the items in the added
order,
employee Theo has internal number 8500
employee Zack has internal number 8501
employee Robert has internal number 8502
employee John has internal number 8503
employee Alec has internal number 8504
employee Carl has internal number 8505

Sortedlist returns
employee Alec has internal number 8504
employee Carl has internal number 8505
employee John has internal number 8503
employee Robert has internal number 8502

employee Theo has internal number 8500
employee Zack has internal number 8501

Marc


Russell Jones said:
Dictionary objects (and Hashtables) don't guarantee a specific object
ordering. The documentation states that items are organized "based on the
hash code of the key. Subsequent lookups of the key use the hash code of the
key to search in only one particular bucket, thus substantially reducing the
number of key comparisons required to find an element."

If you need the specific object ordering, consider using the SortedList
class instead as your base class instead, which also accepts key-value
pairs.


marc said:
I want to create a strongly typed collection and use DictionaryBase

I add a number of objects but when i loop through the list the data is
ordered in a strange maner. How is this possible?

Can anybody tell my why the order gets changed?

Thanks Marc

'***************** OUTPUT **************************

employee Zack has internal number 8501
employee Robert has internal number 8502
employee John has internal number 8503
employee Alec has internal number 8504
employee Carl has internal number 8505
employee Theo has internal number 8500



'***************** CODE **************************

Module Module1

Sub Main()
Dim myEmployees As New cPersons
myEmployees.Add(New cPerson("Theo", "8500"))
myEmployees.Add(New cPerson("Zack", "8501"))
myEmployees.Add(New cPerson("Robert", "8502"))
myEmployees.Add(New cPerson("John", "8503"))
myEmployees.Add(New cPerson("Alec", "8504"))
myEmployees.Add(New cPerson("Carl", "8505"))

Try
For Each employee As cPerson In myEmployees.List
Console.WriteLine("employee {0} has internal number {1}",
employee.Name, employee.InternalPhoneNumber)
Next

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module

Public Class cPerson

Dim _key, _name, _internalPhone As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property

Public Property InternalPhoneNumber() As String
Get
Return _internalPhone
End Get
Set(ByVal Value As String)
_internalPhone = Value
End Set
End Property

Public Sub New(ByVal name As String, ByVal adres As String)
_name = name
_internalPhone = adres
_key = name
End Sub

Friend Property key() As String
Get
Return _key
End Get
Set(ByVal Value As String)
_key = Value
End Set
End Property
End Class
Public Class cPersons
Inherits DictionaryBase

Public Function Add(ByVal person As cPerson) As Boolean
If Not dictionary.Contains(person.key) Then
dictionary.Add(CType(person.key, String), person)
Debug.WriteLine("object added: " & person.key)
Add = True
End If
End Function

Public Function RemoveAll() As Boolean
dictionary.Clear()
Return True
End Function
Public Function Remove(ByVal key As String) As Boolean
If dictionary.Contains(key) Then
dictionary.Remove(key)
Debug.WriteLine("object removed: " & key)
Return True
End If
End Function

Public Function Remove(ByVal person As cPerson) As Boolean
If dictionary.Contains(person.key) Then
dictionary.Remove(person.key)
Debug.WriteLine("object removed: " & person.key)
Return True
End If
End Function

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

Default Public ReadOnly Property Item(ByVal key As [String]) As cPerson
Get
Return CType(Dictionary(key), cPerson)
End Get
End Property

Public ReadOnly Property List() As System.Collections.ICollection
Get
Return InnerHashtable.Values
End Get
End Property

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 

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