开发者

Strongly Typed Dictionary Class in VB.NET

开发者 https://www.devze.com 2023-01-26 06:08 出处:网络
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)

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

精彩评论

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