我有一个附加字典的功能。只要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
当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
这里的问题是,当此函数结束时,dicTitle
和dicFound
被清除,findReplace
函数被提供空参数。
无论如何都要使代码工作或一个好的解决方法?
答案 0 :(得分:8)
您需要添加对Microsoft Scripting Runtime的引用,以便像我使用的那样使用绑定。它最好是你声明它们的方式,因为你可以更好地访问对象,并且可以更容易地看到成员变量等。
您可以这样做的一种方法是为您正在使用的脚本词典创建模块级变量:
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
您也可以通过制作词典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