我试图在VBS中创建字典,并且我可以让它工作;但是,似乎我的子级别字典是通过引用而不是通过值来访问的。
我试过这个:
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")当我更改其中一个值时,它会更改所有的值,这使我认为我的元素字典是引用的。有什么办法能让这一切发生在价值上吗?我希望每个子词典都是不同的。
发布于 2016-03-23 15:01:00
你只有
Set dictElements = CreateObject("Scripting.Dictionary")一次,你只创建一个子字典--并且设置两个键来指向那个子字典。相反,请执行以下操作:
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", dictElementsVBScript有基于引用计数的垃圾收集。当您将第一个字典添加到顶级字典时,顶级字典现在会维护对它的引用。因此,当您将dictElements设置为第二个字典时,顶层字典会使原始字典保持活动状态,因此它不是垃圾收集的。
https://stackoverflow.com/questions/36181559
复制相似问题