如何将哈希表添加为另一个哈希表中的对象 Option Strict On 已设置?
How can I add a Hashtable as an object in another hashtable Option Strict On is set?
我正在尝试在 VB.net 中创建一个包含一组 key/value 对的对象,其值可以是字符串、整数或包含 key/value 的其他对象对。
以下代码按我的预期运行;我可以将 key/values 添加到哈希表,包括另一个我也可以将 key/values 添加到的哈希表。我还可以从对象中读取值。
Dim ht As Hashtable = New Hashtable
ht.Add("Item1", "Item1 Description")
ht.Add("Item2", 42)
ht.Add("Item3", New Hashtable)
ht.Item("Item3").Add("Item3a", "Item3a Description")
ht.Item("Item3").Add("Item3b", "Item3b Description")
Console.WriteLine(ht.Item("Item3")("Item3b"))
但是,这仅在未设置 'Option Strict' 时有效。添加 Option Strict On
给我一个错误:“BC30574 Option Strict On disallows late binding”。
我的问题是,'How can I set a hashtable as a value inside another hashtable when Option Strict On is set?'
如果是导致问题的选择,我愿意使用哈希表以外的集合。我没有设置 Option Strict Off 的选项,因为这是针对 Ui 路径工作流程的 'invoke code' 块。
谢谢!
有不同的方法,如下代码所示:
Dim ht As Hashtable = New Hashtable
ht.Add("Item1", "Item1 Description")
ht.Add("Item2", 42)
ht.Add("Item3", New Hashtable From {{"Item3_1", "Item3_1 Description"},
{"Item3_2", "Item3_3 Description"},
{"Item3_3", "Item3_3 Description"}})
CType(ht.Item("Item3"), Hashtable).Add("Item3_4", "Item3_4 Description")
CType(ht.Item("Item3"), Hashtable).Item("Item3_1") = "Another description for Item3_1"
ht.Add("Item4", New List(Of String) From {"Element1", "Element2"})
我正在尝试在 VB.net 中创建一个包含一组 key/value 对的对象,其值可以是字符串、整数或包含 key/value 的其他对象对。
以下代码按我的预期运行;我可以将 key/values 添加到哈希表,包括另一个我也可以将 key/values 添加到的哈希表。我还可以从对象中读取值。
Dim ht As Hashtable = New Hashtable
ht.Add("Item1", "Item1 Description")
ht.Add("Item2", 42)
ht.Add("Item3", New Hashtable)
ht.Item("Item3").Add("Item3a", "Item3a Description")
ht.Item("Item3").Add("Item3b", "Item3b Description")
Console.WriteLine(ht.Item("Item3")("Item3b"))
但是,这仅在未设置 'Option Strict' 时有效。添加 Option Strict On
给我一个错误:“BC30574 Option Strict On disallows late binding”。
我的问题是,'How can I set a hashtable as a value inside another hashtable when Option Strict On is set?'
如果是导致问题的选择,我愿意使用哈希表以外的集合。我没有设置 Option Strict Off 的选项,因为这是针对 Ui 路径工作流程的 'invoke code' 块。
谢谢!
有不同的方法,如下代码所示:
Dim ht As Hashtable = New Hashtable
ht.Add("Item1", "Item1 Description")
ht.Add("Item2", 42)
ht.Add("Item3", New Hashtable From {{"Item3_1", "Item3_1 Description"},
{"Item3_2", "Item3_3 Description"},
{"Item3_3", "Item3_3 Description"}})
CType(ht.Item("Item3"), Hashtable).Add("Item3_4", "Item3_4 Description")
CType(ht.Item("Item3"), Hashtable).Item("Item3_1") = "Another description for Item3_1"
ht.Add("Item4", New List(Of String) From {"Element1", "Element2"})