开发者

How do I implement IDictionary(Of TKey, TValue) in VB?

I want to create an implementation of the IDictionary(Of TKey, TValue) interface to provide some wrapper functionality.

Public Class ExtendedDictionary(Of TKey, TValue)
    Implements IDictionary(Of TKey, TValue)

    Private _Dictionary As Dictionary(Of TKey, TValue)

    Public Sub Add(ByVal key As TKey, ByVal value As T) Implements IDictionary(Of TKey, T).Add
    'Impl Here'
    End Sub

End Class

When I do this it moans about the methods which I have n开发者_Go百科ot Implemented. When I select to implement methods of ICollection the ICollection are added but then I get a whole stack of errors. Some of them are complaining about methods with identical signatures. For example:

Public ReadOnly Property IsReadOnly() As Boolean Implements ICollection(Of KeyValuePair(Of TKey, T)).IsReadOnly
        Get
            Return IsReadOnly
        End Get
    End Property

So that error I think is pretty easy to fix. Just change the name of the method because we tell it which method it implements so the name should be inconsequential. So I change it to something Lick CollectionIsReadOnly. The harder problems are like these:

    ReadOnly Public Property Count() As Integer Implements ICollection(Of T).Count
        Get
            Return Count
        End Get
    End Property

The error is Interface 'System.Collections.Generic.ICollection(Of T) is not implemented by this class. I am not sure what to do about this error. I don't even really understand it. I trade to change the (Of T) to (Of TValue) but that didn't help.


The proper declaration is:

  Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of TKey, TValue)).Count
    Get
      ' implementation here...
    End Get
  End Property

IntelliSense can help you get these declarations just right, but it is pretty flaky. Start out by writing this:

Public Class ExtendedDictionary(Of TKey, TValue)
End Class

Cursor up and insert this line:

Implements IDictionary(Of TKey, TValue)

When you press Enter after the final ) then the IDE will automatically insert all required methods and properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜