Scripting.Dictionary 使用 Option Script 不允许后期绑定
Scripting.Dictionary with Option Script disallows late binding
我已将 VB6 代码迁移到 Vb.Net 并转为 Option strict On
。
现在,.Keys(intIndex)
抛出异常 Option Strict On disallows late binding
。下面是代码:
Public Function PopulateList(ByRef dicListValues As Scripting.Dictionary)
Dim strKey As String
With dicListValues
For intIndex = 0 To .Count - 1
strKey = .Keys(intIndex)
Next
End With
这就是您不想使用 Scripting.Dictionary
的原因
Dim dict As Scripting.Dictionary = New Scripting.DictionaryClass()
dict.Add("1", 1)
dict.Add(2, "2")
dict.Add(Math.PI, "a")
dict.Add(New Text.StringBuilder(), "builder")
For i = 0 To dict.Count - 1
Console.WriteLine("Key: {0}, Value: {1}", dict.Keys(i), dict(dict.Keys(i)))
Next
你可以看到 dict.Keys(i)
和 dict(dict.Keys(i))
(字典的键和值)可以是任何类型,它是一个对象(每种类型)。
好的,假设您拥有所有字符串。
Dim strKey As String
With dicListValues
For intIndex = 0 To .Count - 1
strKey = .Keys(intIndex).ToString()
Next
End With
(只加字符串就可以控制了,不过要注释好才看得出来)
更好的解决方案是用 Dictionary(Of TKey, TValue) in your entire project. You can convert your Scripting.Dictionary to them with the link I commented before Convert VB6 Scripting.Dictionary to .NET Generic Dictionary
替换您的 Scripting.Dictionary
这里是答案,如果你想使用 Scripting.Dictionary 在 option strict 选项打开时工作。
Dim dict As New Scripting.Dictionary '= New Scripting.DictionaryClass()
Dim str As String
dict.Add("1", 10)
dict.Add(2, "20")
dict.Add(Math.PI, "Test")
dict.Add(New Text.StringBuilder(), "builder")
For i = 0 To dict.Count - 1
str = CType(DirectCast(dict.Keys, Object())(i), String)
Next
我已将 VB6 代码迁移到 Vb.Net 并转为 Option strict On
。
现在,.Keys(intIndex)
抛出异常 Option Strict On disallows late binding
。下面是代码:
Public Function PopulateList(ByRef dicListValues As Scripting.Dictionary)
Dim strKey As String
With dicListValues
For intIndex = 0 To .Count - 1
strKey = .Keys(intIndex)
Next
End With
这就是您不想使用 Scripting.Dictionary
的原因Dim dict As Scripting.Dictionary = New Scripting.DictionaryClass()
dict.Add("1", 1)
dict.Add(2, "2")
dict.Add(Math.PI, "a")
dict.Add(New Text.StringBuilder(), "builder")
For i = 0 To dict.Count - 1
Console.WriteLine("Key: {0}, Value: {1}", dict.Keys(i), dict(dict.Keys(i)))
Next
你可以看到 dict.Keys(i)
和 dict(dict.Keys(i))
(字典的键和值)可以是任何类型,它是一个对象(每种类型)。
好的,假设您拥有所有字符串。
Dim strKey As String
With dicListValues
For intIndex = 0 To .Count - 1
strKey = .Keys(intIndex).ToString()
Next
End With
(只加字符串就可以控制了,不过要注释好才看得出来)
更好的解决方案是用 Dictionary(Of TKey, TValue) in your entire project. You can convert your Scripting.Dictionary to them with the link I commented before Convert VB6 Scripting.Dictionary to .NET Generic Dictionary
替换您的 Scripting.Dictionary这里是答案,如果你想使用 Scripting.Dictionary 在 option strict 选项打开时工作。
Dim dict As New Scripting.Dictionary '= New Scripting.DictionaryClass()
Dim str As String
dict.Add("1", 10)
dict.Add(2, "20")
dict.Add(Math.PI, "Test")
dict.Add(New Text.StringBuilder(), "builder")
For i = 0 To dict.Count - 1
str = CType(DirectCast(dict.Keys, Object())(i), String)
Next