1

我有一个数组,在一个 while 循环中,我将一行数据插入到字典中,然后将每个字典添加到数组中。

我的设置是这样的:

while something

    Dim MyDict as new Scripting.Dictionary
    MyDict.RemoveAll


    'Add data
    MyDict.add "something","something"


    If Count = 0 Then
        ReDim MyArray(0)
    Else
        ReDim Preserve MyArray(UBound(MyArray, 1) + 1)
    End If

    'Add the dictionary to the array of dictionaries
    Set MyArray(UBound(MyArray, 1)) = MyDict

wend

然而,在 while 循环结束时,整个字典数组都指向同一个字典——最后一个字典。我想通过在while循环中声明字典并使用New,以及removeall,我会避免这种情况......

如何确保每本字典不仅仅是对最后插入的字典的引用?

4

1 回答 1

2

这就是科瓦格斯告诉你的:

Dim MyDict as Scripting.Dictionary

while something      

    Set MyDict = new Scripting.Dictionary     

    'Add data     
    MyDict.add "something","something"       

     If Count = 0 Then
         ReDim MyArray(0)
     Else
         ReDim Preserve MyArray(UBound(MyArray, 1) + 1)
     End If      

    'Add the dictionary to the array of dictionaries     
    Set MyArray(UBound(MyArray, 1)) = MyDict  

wend 

...尽管我认为您可能会发现将集合用于这个数组的 insted 会更容易。

于 2012-09-11T16:21:38.530 回答