VB.NET: How to overload Dictionary.Item(TKey)
I created my own Dictionary class, based on 开发者_运维技巧the .NET Dictionary.
Now I want to overload then Item() Method. This works fine with the following code:
Public Overloads Property Item(ByVal key As TKey) As TValue
Get
Return Nothing
End Get
Set(ByVal value As TValue)
MyBase.Item(key) = value
End Set
End Property
When I now use Dictionary.Item(Key) to access a value I get Nothing. But when I use the Dictionary(Key) syntax I get the value and not Nothing.
How can I correctly overload Dictionary.Item(Key) AND Dictionary(Key)?
Thank you Torben
The property must be marked Default
to enable the indexer syntax.
Public Overloads Default Property Item(ByVal key As TKey) As TValue
Get
Return Nothing
End Get
Set(ByVal value As TValue)
MyBase.Item(key) = value
End Set
End Property
精彩评论