I'm trying to create a dictionary of dictionaries in VBS, and I can get it to work; however, it seems that my sub level dictionary is being accessed by reference instead of by value?
I tried this:
Dim s, fso, f, ts, str, fRead, line, i, dictElements, dictionary, screenItem
Set s = CreateObject("System.Text.StringBuilder")
Set fso = CreateObject("Scripting.FileSystemObject")
Set dictElements = CreateObject("Scripting.Dictionary")
Set dictionary = CreateObject("Scripting.Dictionary")
'add elements to dictionary
dictElements.Add "Name", "MyName"
dictElements.Add "Setpoint", 100.0
dictElements.Add "Process Value", 80.6
'Create Data Structure
dictionary.Add "DigitalInputOne", dictElements
dictionary.Add "DigitalInputTwo", dictElements
'test dictionary
dictionary("DigitalInputTwo")("Name")= "Hello"
dictionary("DigitalInputTwo")("Setpoint")= 40.123
HmiRuntime.Screens("Home").ScreenItems("Text field_1").Text = dictionary ("DigitalInputOne")("Name")
HmiRuntime.Screens("Home").ScreenItems("Text field_2").Text = dictionary("DigitalInputOne")("Setpoint")
HmiRuntime.Screens("Home").ScreenItems("Text field_3").Text = dictionary("DigitalInputOne")("Process Value")
HmiRuntime.Screens("Home").ScreenItems("Text field_4").Text = dictionary("DigitalInputTwo")("Name")
HmiRuntime.Screens("Home").ScreenItems("Text field_5").Text = dictionary("DigitalInputTwo")("Setpoint")
HmiRuntime.Screens("Home").ScreenItems("Text field_6").Text = dictionary("DigitalInputTwo")("Process Value")
When I change one of the values it changes all of the values, which makes me think my elements dictionary is by reference. Is there a way to make this happen by value? I want each sub dictionary to be different.
VBScript Dictionary Objects. A Dictionary object can be compared to a PERL associative array. Any Values can be stored in the array and each item is associated with a unique key. The key is used to retrieve an individual element and it is usually an integer or a string, but can be anything except an array.
The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array. The following code illustrates how to create a Dictionary object. Dim d 'Create a variable Set d = CreateObject ("Scripting.Dictionary") d.Add "a", "Athens" 'Add some keys and items d.Add "b", "Belgrade" d.Add "c", "Cairo" ...
A Dictionary object is the equivalent of a PERL associative array. Items, which can be any form of data, are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.
The Dictionary object might seem similar to Arrays, however, the Dictionary object is a more desirable solution to manipulate related data. The following example creates a Dictionary object, adds some key/item pairs to it, and retrieves the item value for the key gr:
You only have
Set dictElements = CreateObject("Scripting.Dictionary")
once so you are only creating one subdictionary -- and are setting two keys to point to that one subdictionary. Instead, do the following:
Set dictElements = CreateObject("Scripting.Dictionary") 'create first sub-dict
dictionary.Add "DigitalInputOne", dictElements
Set dictElements = CreateObject("Scripting.Dictionary") 'create second sub-dict
dictionary.Add "DigitalInputTwo", dictElements
VBScript has reference-counting based garbage collection. When you add the first dictionary to the top-level dictionary, the top level dictionary now maintains a reference to it. Thus, when you set dictElements
equal to a second dictionary, the original dictionary is kept alive by the top-level dictionary, hence it isn't garbage-collected.
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