首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建词典词典vbscript

如何创建词典词典vbscript
EN

Stack Overflow用户
提问于 2016-03-23 14:55:46
回答 1查看 2.3K关注 0票数 5

我试图在VBS中创建字典,并且我可以让它工作;但是,似乎我的子级别字典是通过引用而不是通过值来访问的。

我试过这个:

代码语言:javascript
复制
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")

当我更改其中一个值时,它会更改所有的值,这使我认为我的元素字典是引用的。有什么办法能让这一切发生在价值上吗?我希望每个子词典都是不同的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-23 15:01:00

你只有

代码语言:javascript
复制
Set dictElements = CreateObject("Scripting.Dictionary")

一次,你只创建一个子字典--并且设置两个键来指向那个子字典。相反,请执行以下操作:

代码语言:javascript
复制
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有基于引用计数的垃圾收集。当您将第一个字典添加到顶级字典时,顶级字典现在会维护对它的引用。因此,当您将dictElements设置为第二个字典时,顶层字典会使原始字典保持活动状态,因此它不是垃圾收集的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36181559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档