问题用另一个字典类型的变量初始化一个字典类型的变量
Issue initialising one variable of type dictionary with another variabjle of type dict
Dim globalDict
Dim localDict
.
'Data from a excel is loaded to globalDict
Set localDict=globalDict(1)
localDict(item1)="AAA"
此更新也会更新 globalDict
中的值。就好像 localDict
只是一个指针。
知道哪里出了问题吗?
谢谢,
拉杰什
这是设计使然:请参阅 Set Statement under Statements (VBScript):
Generally, when you use Set
to assign an object reference to a
variable, no copy of the object is created for that variable.
Instead, a reference to the object is created. More than one
object variable can refer to the same object. Because these variables
are references to (rather than copies of) the object, any change in
the object is reflected in all variables that refer to it.
您可以制作 Dictionary 对象的相同副本,如下所示:
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim globalDict
Set globalDict = CreateObject("Scripting.Dictionary")
globalDict.Add "a", "Athens" ' add some keys and items
globalDict.Add "b", "Belgrade"
globalDict.Add "c", "Cairo"
' create an identical copy of a Dictionary object
Dim localDict, arrKeys, arrItems, ii ' declare variables
Set localDict = CreateObject("Scripting.Dictionary")
arrKeys = globalDict.Keys ' get the keys
'arrItems = globalDict.Items ' get the items: unnecessary
For ii= 0 To UBound( arrKeys)
'(debug output) strResult = strResult & vbNewLine & arrKeys(ii) & vbTab & arrItems(ii)
localDict.Add arrKeys(ii), globalDict( arrKeys(ii)) ' add appropriate keys and items
Next
' identical copy is created now
localDict("b") = "Brno"
strResult = strResult & vbNewLine & globalDict("b")
strResult = strResult & vbNewLine & localDict("b")
strResult = strResult & vbNewLine & "-"
'strResult = strResult & vbNewLine &
Wscript.Echo strResult ' the only `Echo` => run under `CScript.exe` or `WScript.exe`
输出:
==> cscript D:\VB_scripts\SO644677.vbs
37644677.vbs
Belgrade
Brno
-
Dim globalDict
Dim localDict
.
'Data from a excel is loaded to globalDict
Set localDict=globalDict(1)
localDict(item1)="AAA"
此更新也会更新 globalDict
中的值。就好像 localDict
只是一个指针。
知道哪里出了问题吗?
谢谢, 拉杰什
这是设计使然:请参阅 Set Statement under Statements (VBScript):
Generally, when you use
Set
to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because these variables are references to (rather than copies of) the object, any change in the object is reflected in all variables that refer to it.
您可以制作 Dictionary 对象的相同副本,如下所示:
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim globalDict
Set globalDict = CreateObject("Scripting.Dictionary")
globalDict.Add "a", "Athens" ' add some keys and items
globalDict.Add "b", "Belgrade"
globalDict.Add "c", "Cairo"
' create an identical copy of a Dictionary object
Dim localDict, arrKeys, arrItems, ii ' declare variables
Set localDict = CreateObject("Scripting.Dictionary")
arrKeys = globalDict.Keys ' get the keys
'arrItems = globalDict.Items ' get the items: unnecessary
For ii= 0 To UBound( arrKeys)
'(debug output) strResult = strResult & vbNewLine & arrKeys(ii) & vbTab & arrItems(ii)
localDict.Add arrKeys(ii), globalDict( arrKeys(ii)) ' add appropriate keys and items
Next
' identical copy is created now
localDict("b") = "Brno"
strResult = strResult & vbNewLine & globalDict("b")
strResult = strResult & vbNewLine & localDict("b")
strResult = strResult & vbNewLine & "-"
'strResult = strResult & vbNewLine &
Wscript.Echo strResult ' the only `Echo` => run under `CScript.exe` or `WScript.exe`
输出:
==> cscript D:\VB_scripts\SO644677.vbs
37644677.vbs
Belgrade
Brno
-