Friday, November 5, 2010

Observable Dictionary

Here is an Observable Dictionary that I threw together for a friend. It only does what we wanted it to do at that point in time, so you you need something more, add it. :)



Public Class ObservableDictionary(Of T, P)
Implements IDictionary(Of T, P)

Event ItemAdded(ByVal Key As T)
Event ItemRemoved(ByVal Key As T, ByVal value As P, ByVal Success As Boolean)
Event ItemChanged(ByVal Key As T, ByVal OldValue As P, ByVal NewValue As P)

Dim _BaseDictionary As Dictionary(Of T, P)

Sub New()
_BaseDictionary = New Dictionary(Of T, P)
End Sub

Public Sub Clear() Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).Clear
_BaseDictionary.Clear()
End Sub

Public Function Contains(ByVal item As System.Collections.Generic.KeyValuePair(Of T, P)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).Contains
Return _BaseDictionary.Contains(item)
End Function

Public Sub CopyTo(ByVal array() As System.Collections.Generic.KeyValuePair(Of T, P), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).CopyTo
Throw New NotImplementedException
End Sub

Public ReadOnly Property Count As Integer Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).Count
Get
Return _BaseDictionary.Count
End Get
End Property

Public ReadOnly Property IsReadOnly As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).IsReadOnly
Get
Throw New NotImplementedException
End Get
End Property

Public Function RemovePair(ByVal item As System.Collections.Generic.KeyValuePair(Of T, P)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).Remove
Dim Value As P = _BaseDictionary(item.Key)
Dim Success = _BaseDictionary.Remove(item.Key)

RaiseEvent ItemRemoved(item.Key, Value, Success)
Return Success

End Function



Public Function ContainsKey(ByVal key As T) As Boolean Implements System.Collections.Generic.IDictionary(Of T, P).ContainsKey
Return _BaseDictionary.ContainsKey(key)
End Function

Default Public Property Item(ByVal key As T) As P Implements System.Collections.Generic.IDictionary(Of T, P).Item
Get

Return _BaseDictionary.Item(key)
End Get
Set(ByVal value As P)
Dim oldvalue As P = _BaseDictionary.Item(key)
_BaseDictionary.Item(key) = value
RaiseEvent ItemChanged(key, oldvalue, value)
End Set
End Property

Public ReadOnly Property Keys As System.Collections.Generic.ICollection(Of T) Implements System.Collections.Generic.IDictionary(Of T, P).Keys
Get
Return _BaseDictionary.Keys
End Get
End Property

Public Function Remove(ByVal key As T) As Boolean Implements System.Collections.Generic.IDictionary(Of T, P).Remove

Dim value = _BaseDictionary(key)
Dim Success = _BaseDictionary.Remove(key)
RaiseEvent ItemRemoved(key, Value, Success)

Return Success

End Function

Public Function TryGetValue(ByVal key As T, ByRef value As P) As Boolean Implements System.Collections.Generic.IDictionary(Of T, P).TryGetValue
If _BaseDictionary.ContainsKey(key) Then
value = _BaseDictionary(key)
Return True
Else
value = Nothing
Return False
End If
End Function

Public ReadOnly Property Values As System.Collections.Generic.ICollection(Of P) Implements System.Collections.Generic.IDictionary(Of T, P).Values
Get
Return _BaseDictionary.Values
End Get
End Property

Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Collections.Generic.KeyValuePair(Of T, P)) Implements System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of T, P)).GetEnumerator
Return _BaseDictionary.GetEnumerator
End Function

Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Throw New NotImplementedException
End Function

Public Sub AddPair(ByVal item As System.Collections.Generic.KeyValuePair(Of T, P)) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of T, P)).Add

_BaseDictionary.Add(item.Key, item.Value)
RaiseEvent ItemAdded(item.Key)
End Sub

Public Sub Add(ByVal key As T, ByVal value As P) Implements System.Collections.Generic.IDictionary(Of T, P).Add
_BaseDictionary.Add(key, value)

RaiseEvent ItemAdded(key)
End Sub
End Class


Add to Google