Implement IDictionary or Inherit DictionaryBase?

Evan1993

Well-known member
Joined
Apr 2, 2008
Messages
47
Programming Experience
1-3
If I use a normal dictionary(like the code below), it works great.
VB.NET:
        Dim Example As New Dictionary(Of String, MyObject)
        For Each pair As KeyValuePair(Of String, MyObject) In Example
            'Do stuff.
        Next

However I want to add a few properties and subs, so I want to make a customized dictionary which will work the same as a normal dictionary.

Do I want to make a custom class that Implements IDictionary or Inherits DictionaryBase? Or both? (I couldn't find any examples using both)

Edit: Hmm trying to figure it out with a Point Dictionary. Still not sure if I'm doing it right, heres what I got so far.

VB.NET:
Public Class CustomDictionary
    Inherits DictionaryBase
    Implements IDictionary(Of String, Point)


    Public Sub Add(ByVal item As System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).Add
        Me.Dictionary.Add(item.Key, item.Value)
    End Sub

    Public Overloads Sub Clear() Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).Clear
        Me.Dictionary.Clear()
    End Sub

    Public Function Contains(ByVal item As System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).Contains
        If Me.Dictionary.Contains(item.Key) = True Then
            Return Me.Dictionary.Item(item.Key) = item.Value
        End If
        Return False
    End Function

    Public Sub CopyTo(ByVal array() As System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).CopyTo
        '?
    End Sub

    Public Overloads ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).Count
        Get
            Return Me.Dictionary.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).IsReadOnly
        Get
            Return False
        End Get
    End Property

    Public Function Remove(ByVal item As System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).Remove
        Me.Dictionary.Remove(item.Key)
    End Function

    Public Sub Add1(ByVal key As String, ByVal value As System.Drawing.Point) Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).Add
        Me.Dictionary.Add(key, value)
    End Sub

    Public Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).ContainsKey
        Return Me.Dictionary.Contains(key)
    End Function

    Default Public Property Item(ByVal key As String) As System.Drawing.Point Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).Item
        Get
            Return Me.Dictionary.Item(key)
        End Get
        Set(ByVal value As System.Drawing.Point)
            Me.Dictionary.Item(key) = value
        End Set
    End Property

    Public ReadOnly Property Keys() As System.Collections.Generic.ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).Keys
        Get
            Return Me.Dictionary.Keys
        End Get
    End Property

    Public Function Remove1(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).Remove
        Me.Dictionary.Remove(key)
    End Function

    Public Function TryGetValue(ByVal key As String, ByRef value As System.Drawing.Point) As Boolean Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).TryGetValue
        If Me.Dictionary.Contains(key) Then
            If (Me.Dictionary.Item(key) = Nothing) = False Then
                value = Me.Dictionary.Item(key)
                Return True
            End If
            Return False
        End If
        Return False
    End Function

    Public ReadOnly Property Values() As System.Collections.Generic.ICollection(Of System.Drawing.Point) Implements System.Collections.Generic.IDictionary(Of String, System.Drawing.Point).Values
        Get
            Return Me.Dictionary.Keys
        End Get
    End Property

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)) Implements System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, System.Drawing.Point)).GetEnumerator
        '?
    End Function

    Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        '?
    End Function
End Class
 
Last edited:
Back
Top