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
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