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:
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:
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.
Is there anyway to makes to code work, or a good workaround?
Basic setup of a VBA Dictionary For early binding, start by opening the Visual Basic Editor by pressing Alt+F11 and going to Tools > References. Set a reference to the Microsoft Scripting Runtime object library.
You can assign a Key to an Item when you add the Item to the Collection, but you cannot retrieve the Key associated with an Item nor can you determine (directly) whether a key exists in a Collection. Dictionaries are much friendly and open with their keys. Dictionaries are also considerably faster than Collections.
To use VBA Dictionary, we need to enable Microsoft Scripting Runtime function which is available in Tool menu option under References. Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more. VBA Dictionary has a few major properties as explained below; Count = It returns the count of a number of variable in the dictionary key.
Find Microsoft Scripting Runtime in the list and place a check in the box beside it. Creating a Dictionary in this way is called “Early Binding”. There is also “Late Binding”. Let’s have a look at what this means.
This is a simple example but it shows how useful a Dictionary is. We will see a real world example later in the post. Let’s look at the basics of using a Dictionary. To use the Dictionary you need to first add the reference. Select Tools->References from the Visual Basic menu.
As in a normal word dictionary, we see words with their meaning, categories, application, pronunciation, etc. In VBA Dictionary as well we can define all the necessary characteristics and value which we want to assign to any word or variable.
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.
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
You can also do this by making the dictionaries static. This will preserve them between function calls like the documentation says.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With