开发者

VB.NET: How to overload Dictionary.Item(TKey)

开发者 https://www.devze.com 2023-02-09 17:41 出处:网络
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:

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
0

精彩评论

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