多种类型的VB.NET字典 [英] VB.NET dictionary of multiple types
问题描述
我对 VB.NET 非常陌生,而且我有 Objective-C 背景.
I am very new to VB.NET, and I come from an Objective-C background.
在 Objective-C 中,我可以创建一个 NSMutableDictionary
来保存任何类型的值.
In Objective-C, I could create an NSMutableDictionary
which could hold any types of values.
在这里使用 VB.NET,我设法制作了一个 Dictionary
,但是当我初始化它的一个实例时,我被要求提供一个特定的值类型.如何在一个字典中启用任何值类型?
Here with VB.NET, I managed to make a Dictionary
, but when I initialized an instance of it, I am asked for a specific value type. How can I enable any value types within one single dictionary?
推荐答案
你可以写一个字典来保存这样的对象:
You can write a dictionary to hold objects like this:
Dim myDictionary As New Dictionary(Of String, Object)
然后提供它:
myDictionary.Add("keyA", New Button)
myDictionary.Add("keyB", "Test")
这可能不被认为是最佳编程实践.最好利用泛型并让字典保存项目的公共类或接口.
This is probably not considered best programming practice. It's better to take advantage of generics and have a dictionary hold a common class or interface of items.
为每个项目的类或接口使用多个字典会更高效且更易于维护.
Having multiple dictionaries for each class or interface of items would be more efficient and easier to maintain.
这篇关于多种类型的VB.NET字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!