在 Visual Basic for Applications (VBA) 中设置全局 Scripting.Dictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22382729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a global Scripting.Dictionary in Visual Basic for Applications (VBA)
提问by Martijn Nosyncerror
I have a function that is appending a dictionary. I want to keep the contents of this dictionary as long as the updateList
isn't true. I currently have it setup like this:
我有一个附加字典的函数。只要这updateList
不是真的,我就想保留这本词典的内容。我目前有这样的设置:
Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
Dim dicTitle As Variable
Dim dicFound As Variable
If updateList = True Then
Set dicTitle = CreateObject("Scripting.Dictionary")
Set dicFound = CreateObject("Scripting.Dictionary")
While status
Set ObjectsJSON = jsonlibPollux.parse(responseString)
With dicTitle
.Add inc, ObjectsJSON.Item("title")
End With
With dicFound
.Add inc, ObjectsJSON.Item("description")
End With
Wend
End If
And when voerUit
is true the following happens:
当voerUit
为真时,会发生以下情况:
For i = 1 To dicTitle.Count
klaar = findReplace(dicTitle.Item(i), "x" + dicTitle.Item(i), dicTitle.Item(i) + vbCrLf + vbCrLf + dicFound.Item(i))
Next i
The problem here is that when this function ends, dicTitle
and dicFound
are cleared, and the findReplace
function gets fed empty arguments.
这里的问题是,当这个函数结束dicTitle
并被dicFound
清除时,该findReplace
函数会得到空参数。
Is there anyway to makes to code work, or a good workaround?
有没有办法让代码工作,或者一个好的解决方法?
回答by enderland
You will need to add a reference to the Microsoft Scripting Runtime to use binding like I have used. It is preferable to the manner you are declaring them as you have better access to the object and can see member variables easier, etc.
您将需要添加对 Microsoft Scripting Runtime 的引用,才能像我使用的那样使用绑定。这比声明它们的方式更可取,因为您可以更好地访问对象并且可以更容易地查看成员变量等。
Method 1 - module level or public variables for dictionaries
方法 1 - 字典的模块级或公共变量
One way you can do this is to create module level variables for the scripting dictionaries you are using:
一种方法是为您正在使用的脚本字典创建模块级变量:
Public gDicTitle As Scripting.Dictionary
Public gDicFound As Scripting.Dictionary
Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
If updateList = True Then
Set gDicTitle = New Scripting.Dictionary
Set gDicFound = New Scripting.Dictionary
While Status
Set ObjectsJSON = jsonlibPollux.Parse(responseString)
With gDicTitle
.Add inc, ObjectsJSON.Item("title")
End With
With gDicFound
.Add inc, ObjectsJSON.Item("description")
End With
Wend
End If
End Function
Method 2 - static references to dictionaries
方法 2 - 对字典的静态引用
You can also do this by making the dictionaries static. This will preserve them between function calls like the documentation says.
您也可以通过使字典static来做到这一点。这将在函数调用之间保留它们,如文档所述。
Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
Static gDicTitle As Scripting.Dictionary
Static gDicFound As Scripting.Dictionary
If updateList = True Then
Set gDicTitle = New Scripting.Dictionary
Set gDicFound = New Scripting.Dictionary
While Status
Set ObjectsJSON = jsonlibPollux.Parse(responseString)
With gDicTitle
.Add inc, ObjectsJSON.Item("title")
End With
With gDicFound
.Add inc, ObjectsJSON.Item("description")
End With
Wend
End If
End Function