我有一本字典,我想用它来存储带有字典的键作为值。 本质上,使用我的数据表的以下屏幕截图,我希望字典(在 json 视图中)为:
{'Folder/PU01': {'PLCName':'PLCCC', 'DeviceName': 'fasasd', 'Description': '', '....'}
{'Folder/PU02': {'PLCName':'', 'DeviceName': '', 'Description': '', '....'}
etc...
我有下面的代码,它基本上是在创建这个,但是 Next 上方的最底线是出错的地方
udtInstancesCurrent.Add deviceTagPath, udtInstanceParamsCurrent
这是将 udtInstanceParamsCurrent
字典分配给 udtInstanceCurrent
键,但由于它是对字典的引用而不是副本,因此下次循环时它会被覆盖。
我的问题是:如何将 udtInstanceCurrent
中字典键的值设置为 udtInstanceParamsCurrent
字典的副本而不是对原始字典的引用?
Dim udtInstancesCurrent As Scripting.Dictionary
Dim udtInstanceParamsCurrent As Scripting.Dictionary
Set udtInstancesCurrent = New Scripting.Dictionary
Set udtInstanceParamsCurrent = New Scripting.Dictionary
'''' SAVE PARAMETER VALUES ''''
' For each udt instance tag path, add its param value into a dictionary to save its values
For Each udtTagPathCell In Range(.Cells(INSTANCES_ROW_HEADERS + 1, INSTANCES_COL_UDTTAGPATH), .Cells(INSTANCES_ROW_HEADERS, INSTANCES_COL_UDTTAGPATH).End(xlDown))
udtTagPath = udtTagPathCell.Value
deviceName = .Cells(udtTagPathCell.Row, INSTANCES_COL_DEVICENAME)
deviceParentPath = .Cells(udtTagPathCell.Row, INSTANCES_COL_DEVICEPARENTPATH)
deviceTagPath = deviceParentPath & "/" & deviceName
Row = udtTagPathCell.Row
udtInstanceParamsCurrent.RemoveAll
' For each parameter defined, add into a dictionary
For Each param In Range(.Cells(Row, INSTANCES_COL_PARAMSSTART), .Cells(Row, .Cells(INSTANCES_ROW_HEADERS, 1).End(xlToRight).Column))
paramName = .Cells(INSTANCES_ROW_HEADERS, param.Column)
udtInstanceParamsCurrent.Add paramName, param.Value
Next
' TODO: Dictionary is being overwritten. need to set this to a new instance of the dictionary
udtInstancesCurrent.Add deviceTagPath, udtInstanceParamsCurrent
Next
答案 0 :(得分:0)
我已经解决了。我只需要在 for 循环内而不是在循环外将 udtInstanceParamsCurrent
设置为新字典。
Dim udtInstancesCurrent As Scripting.Dictionary
Dim udtInstanceParamsCurrent As Scripting.Dictionary
Set udtInstancesCurrent = New Scripting.Dictionary
'''' SAVE PARAMETER VALUES ''''
' For each udt instance tag path, add its param value into a dictionary to save its values
For Each udtTagPathCell In Range(.Cells(INSTANCES_ROW_HEADERS + 1, INSTANCES_COL_UDTTAGPATH), .Cells(INSTANCES_ROW_HEADERS, INSTANCES_COL_UDTTAGPATH).End(xlDown))
Set udtInstanceParamsCurrent = New Scripting.Dictionary
udtTagPath = udtTagPathCell.Value
deviceName = .Cells(udtTagPathCell.Row, INSTANCES_COL_DEVICENAME)
deviceParentPath = .Cells(udtTagPathCell.Row, INSTANCES_COL_DEVICEPARENTPATH)
deviceTagPath = deviceParentPath & "/" & deviceName
Row = udtTagPathCell.Row
udtInstanceParamsCurrent.RemoveAll
' For each parameter defined, add into a dictionary
For Each param In Range(.Cells(Row, INSTANCES_COL_PARAMSSTART), .Cells(Row, .Cells(INSTANCES_ROW_HEADERS, 1).End(xlToRight).Column))
paramName = .Cells(INSTANCES_ROW_HEADERS, param.Column)
udtInstanceParamsCurrent.Add paramName, param.Value
Next
' TODO: Dictionary is being overwritten. need to set this to a new instance of the dictionary
udtInstancesCurrent.Add deviceTagPath, udtInstanceParamsCurrent
Next