开发者

Strongly Typed Dictionary Class in VB.NET

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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜