Try this (it took me a while). Replace YOUROBJECT (using Find / Replace) with the name of your class. I use events in my collections to notify parent classes when changes occur, so that is an added bonus. :)
I tested the primary collection functions, but testing was not comprehensive.
Hope this helps,
Bg
#Region "YOUROBJECT Collection"
'-------------------------
' Framework Declarations
'-------------------------
Public Enum CollectionItemStatus
Added
Moved
Removed
End Enum
Public Delegate Sub ItemStatusHandler(ByVal sender As Object, ByVal item As Object, ByVal position As Integer, ByVal status As CollectionItemStatus)
'-------------------------
<Serializable()> _
Public Class YOUROBJECTCollection
Inherits SortedList
Public Event StatusMessage As ItemStatusHandler
Public Sub New()
End Sub
Default Public Overloads ReadOnly Property Item(ByVal index As Integer) As YOUROBJECT
Get
Return DirectCast(MyBase.Item(ind
End Get
End Property
Default Public Overloads ReadOnly Property Item(ByVal key As String) As YOUROBJECT
Get
Return DirectCast(MyBase.Item(key
End Get
End Property
Public Overloads Function Add(ByVal key As String, ByVal value As YOUROBJECT)
MyBase.Add(key, value)
RaiseEvent StatusMessage(Me, value, IndexOfKey(key), CollectionItemStatus.Added
End Function
Public Overloads Function Add(ByVal key As String) As YOUROBJECT
Dim value As New YOUROBJECT()
MyBase.Add(key, value)
RaiseEvent StatusMessage(Me, value, IndexOfKey(key), CollectionItemStatus.Added
Return MyBase.Item(key)
End Function
Public Overloads Function Contains(ByVal key As String) As Boolean
Return MyBase.ContainsKey(key)
End Function
Public Overloads Function ContainsKey(ByVal key As String) As Boolean
Return MyBase.ContainsKey(key)
End Function
Public Overloads Function ContainsValue(ByVal value As YOUROBJECT) As Boolean
Return MyBase.ContainsValue(value
End Function
Public Overloads Function IndexOfKey(ByVal key As String) As Integer
Return MyBase.IndexOfKey(key)
End Function
Public Overloads Function IndexOfValue(ByVal value As YOUROBJECT) As Integer
Return MyBase.IndexOfValue(value)
End Function
Public Overloads Sub Remove(ByVal key As String)
Dim obj As YOUROBJECT = MyBase.Item(key)
RaiseEvent StatusMessage(Me, obj, MyBase.IndexOfValue(obj), CollectionItemStatus.Remov
MyBase.Remove(key)
OnRemove(IndexOfKey(key))
End Sub
Public Overloads Sub Remove(ByVal value As YOUROBJECT)
RaiseEvent StatusMessage(Me, value, MyBase.IndexOfValue(value)
MyBase.Remove(MyBase.GetKe
OnRemove(MyBase.IndexOfVal
End Sub
Protected Sub OnRemove(ByVal index As Integer)
Dim i As Integer
For i = index + 1 To Count
RaiseEvent StatusMessage(Me, MyBase.Item(i), MyBase.IndexOfValue(Item(i
Next
End Sub
Public Overrides Sub Clear()
OnClear()
MyBase.Clear()
End Sub
Protected Sub OnClear()
Dim i As Integer
For i = 0 To Count
RaiseEvent StatusMessage(Me, MyBase.Item(i), MyBase.IndexOfValue(Item(i
Next
End Sub
End Class
#End Region
Main Topics
Browse All Topics





by: GoodJunPosted on 2002-12-31 at 05:24:36ID: 7649754
You can use SortList to have both Key and Index. but it is not strongly typed, everything is an Object.