Class 字典内存问题?填充dict、.removeall、填充问题

Class 字典内存问题?填充dict、.removeall、填充问题,class,vba,object,excel,dictionary,Class,Vba,Object,Excel,Dictionary,因此,我在这里使用了一些代码,其中我使用了一个字典来填充作为自定义类中的属性保存的两个不同的字典。我这样做是为了提高效率 注意:对于这个问题,我有一个变通解决方案,为我想要设置的每个属性使用一个字典,但这并不太有效 下面是我的代码: for iKey = 1 to class.maxnumber ' 8 dTempDict.add iKey, cdbl(24) ' enforce to 24 for calcs later next iKey Set class.dict1 = d

因此,我在这里使用了一些代码,其中我使用了一个字典来填充作为自定义类中的属性保存的两个不同的字典。我这样做是为了提高效率

注意:对于这个问题,我有一个变通解决方案,为我想要设置的每个属性使用一个字典,但这并不太有效

下面是我的代码:

for iKey = 1 to class.maxnumber ' 8
    dTempDict.add iKey, cdbl(24)   ' enforce to 24 for calcs later
next iKey

Set class.dict1 = dTempDict ' commit to class.dict1

dTempDict.removeall 'wipe temp dictionary

for iKey = 1 to class.maxnumber ' 8
    dTempDict.add iKey, "word"   ' something other than 24 to test
next iKey

Set class.dict2 = dTempDict
因此,上述方法效果良好。然后我试着循环并打印class.dict1的键,没有问题。然后,当我试图将这些值分配给预先声明的dbl时,我遇到了麻烦。然后我在不同的子传递类byref中循环遍历每个键:

dim dTempDict as scripting.dictionary
Set dTempDict = class.dict1 
for each iKey in dTempDict
 msgbox typename(dTempDict.Item(iKey))
next iKey
这带来了结果。。。“字符串”。。。令人困惑然后我将我的valueholder更改为一个字符串,它起了作用。我在类中检查了我的访问器,它们不会循环回错误的dictionary属性,因此,即使我第二次分配它们,甚至执行.removeall,第二个dictionary的值也会填充到第一个dictionary中

有什么想法吗

如上所述,对class.dict1和class.dict2使用不同的临时字典,它们被正确分配,但这仍然令人困惑

当你这样做的时候

Set class.dict1 = dTempDict 
dTempDict.removeall 
'...
Set class.dict2 = dTempDict
。。。然后
dict1
dict2
都指向同一个Dictionary对象(
dTempDict

将一个对象变量指定给另一个对象变量不会创建该对象的副本,只会导致指向同一对象的额外“指针”


你应该创建一个新词典,而不是重复使用同一个词典来分配给
dict2

一般来说,我不喜欢在课堂上使用词典,除了保存其他课堂。我不知道你的情况,所以我不能对此发表评论。但是不管“24”是什么,您可能会认为它应该是自己的对象,而您的类将包含另一个集合类。顺便说一句,我使用的是集合,而不是字典。然后你可以访问像

clsDepartment.Employees.Count
而不是

clsDepartment.mydict(1)
无论如何,对于您所拥有的,您应该在类中填充字典,而不是创建临时字典。如果你的班级看起来像这样

Private mdcTwentyFour As Scripting.Dictionary
Private mdcNotTwentyFour As Scripting.Dictionary

Public Property Get TwentyFour() As Scripting.Dictionary
    Set TwentyFour = mdcTwentyFour
End Property

Public Property Get NotTwentyFour() As Scripting.Dictionary
    Set NotTwentyFour = mdcNotTwentyFour
End Property

Public Property Get MaxNumber() As Long
    MaxNumber = 8
End Property

Private Sub Class_Initialize()

    Set mdcTwentyFour = New Scripting.Dictionary
    Set mdcNotTwentyFour = New Scripting.Dictionary

End Sub
Sub FillClassDicts()

    Dim clsClass As CClass
    Dim i As Long

    Set clsClass = New CClass

    For i = 1 To clsClass.MaxNumber
        clsClass.TwentyFour.Add i, 24
        clsClass.NotTwentyFour.Add i, "word"
    Next i

    Debug.Print clsClass.TwentyFour.Count, clsClass.NotTwentyFour.Count
    Debug.Print TypeName(clsClass.TwentyFour(1)), TypeName(clsClass.NotTwentyFour(5))

End Sub
那么你的潜艇可能是这样的

Private mdcTwentyFour As Scripting.Dictionary
Private mdcNotTwentyFour As Scripting.Dictionary

Public Property Get TwentyFour() As Scripting.Dictionary
    Set TwentyFour = mdcTwentyFour
End Property

Public Property Get NotTwentyFour() As Scripting.Dictionary
    Set NotTwentyFour = mdcNotTwentyFour
End Property

Public Property Get MaxNumber() As Long
    MaxNumber = 8
End Property

Private Sub Class_Initialize()

    Set mdcTwentyFour = New Scripting.Dictionary
    Set mdcNotTwentyFour = New Scripting.Dictionary

End Sub
Sub FillClassDicts()

    Dim clsClass As CClass
    Dim i As Long

    Set clsClass = New CClass

    For i = 1 To clsClass.MaxNumber
        clsClass.TwentyFour.Add i, 24
        clsClass.NotTwentyFour.Add i, "word"
    Next i

    Debug.Print clsClass.TwentyFour.Count, clsClass.NotTwentyFour.Count
    Debug.Print TypeName(clsClass.TwentyFour(1)), TypeName(clsClass.NotTwentyFour(5))

End Sub