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

DictionaryBase ordering

P: n/a
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


Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <he**@msn.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #2

P: n/a
Check out the PrintKeysAndValues procedure at the web page below:

http://msdn.microsoft.com/library/de...classtopic.asp

It shows how to access the data in sorted order.
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"marc" <he**@msn.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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" <ar**@nospam.northstate.net> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
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" <he**@msn.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.