I'm trying to create a strongly-typed Dictionary class in VB.NET. I'm tired of typing
Dim people as Dictionary(Of String, Person)
and want to make a PersonDictionary class so I can say
Dim people as PersonDictionary
My reference material says to create a new class that inherits the Dictionar开发者_运维技巧yBase class. Then override the Add, Remove, and Item Sub/Properties.
It seems like a pretty common pattern, is there an easier way?
Thanks
This could do this:
Public Class PersonDictionary
Inherits Dictionary(Of String, Person)
End Class
imports MyDictionary = System.Collections.Generic.Dictionary(Of string, string)
public module MyModule
Sub Main()
dim myData as New MyDictionary
mydata.Add("hello", "world")
Console.WriteLine(mydata.Keys.Count)
mydata.Add("hello2", "world2")
Console.WriteLine(mydata.Keys.Count)
End Sub
end module
精彩评论