2011-02-09 30 views
1

我創建了我自己的基於.NET Dictionary的Dictionary類。VB.NET:如何重載Dictionary.Item(TKey)

現在我想重載然後Item()方法。這工作得很好用下面的代碼:

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 

當我現在用Dictionary.Item(密鑰)來訪問值我什麼也沒得到。但是,當我使用Dictionary(Key)語法時,我得到的值不是Nothing。

如何正確重載Dictionary.Item(Key)AND Dictionary(Key)?

謝謝 託本

回答

3

的屬性必須標明Default啓用索引語法。

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

這麼簡單...謝謝:) – Torben 2011-02-09 12:36:15

相關問題