vba copy/clone 对象字典的 class 问题
vba deep copy/clone issue with class object dictionary
我的 Main Sub 中有一本字典(KEY = 字符串;VALUE = Class 对象)。 Class 对象由两个字典组成。当我收集数据并检查存储在字典值(Class 对象 - 字典)中的值时,我注意到只有最后一个值被存储。我的意思是,我的 Main Sub 字典中的所有值都指向相同的字典引用,因此,我的 Class 对象的所有实例都包含相同的数据。这意味着我需要克隆 Class 对象(深拷贝?)。我之前用 Class 只存储简单值的对象成功地做到了这一点,但没有用字典。我需要帮助克隆包含字典的 Class 对象。
主副
Dim dGroup As New Scripting.Dictionary ' Main Dictionary
'
' loop thru a listbox
For i = 0 To UserForm1.ListBox1.ListCount - 1
Gname = UserForm1.ListBox1.List(i) ' get listbox names
' populate temp dictionary
Set dic = FNC.GET_SESSION_FILE_ELEMENTS(mySesFile, Gname)
'
' instantiate new Class Object
Dim NewCol As New cVM_Col
Call NewCol.INIT(dic) ' pass the dictionary to a 'constructor'
dGroup.Add Gname, NewCol.CLONE ' add to the MAIN SUB dictionary
'
Set dic = Nothing ' clear the temp dictionary
Next i
CLASS 对象
Private dElms As Scripting.Dictionary
Private dDat As Scripting.Dictionary
'
Private Sub Class_Initialize()
Set dElms = New Scripting.Dictionary
Set dDat = New Scripting.Dictionary
End Sub
'
Public Sub INIT(inp As Scripting.Dictionary)
Set dElms = inp
End Sub
'
Public Function CLONE()
Set CLONE = New cVM_Col
Set CLONE.dElms = dElms ' <-- THIS IS WHERE IT CRASHES
Set CLONE.dDat = dDat
End Function
通常情况下,当我只克隆简单数据类型(如 String 或 Long 或 Double)时,我的 CLONE 函数会起作用。我从来不需要用字典来做这件事。
要克隆我的 CLASS 对象中的字典对象,我必须进行以下更改:
CLASS 对象
(修改后的 CLONE 函数)
Public Function CLONE()
Set CLONE = New cVM_Col
CLONE.Elms = dElms
CLONE.Dat = dDat
End Function
(添加的属性)
Public Property Get Elms() As Scripting.Dictionary
Set Elms = dElms
End Property
Public Property Let Elms(p As Scripting.Dictionary)
Set dElms = p
End Property
'
Public Property Get Dat() As Scripting.Dictionary
Set Dat = dDat
End Property
Public Property Let Dat(p As Scripting.Dictionary)
Set dDat = p
End Property
我的 Main Sub 中有一本字典(KEY = 字符串;VALUE = Class 对象)。 Class 对象由两个字典组成。当我收集数据并检查存储在字典值(Class 对象 - 字典)中的值时,我注意到只有最后一个值被存储。我的意思是,我的 Main Sub 字典中的所有值都指向相同的字典引用,因此,我的 Class 对象的所有实例都包含相同的数据。这意味着我需要克隆 Class 对象(深拷贝?)。我之前用 Class 只存储简单值的对象成功地做到了这一点,但没有用字典。我需要帮助克隆包含字典的 Class 对象。
主副
Dim dGroup As New Scripting.Dictionary ' Main Dictionary
'
' loop thru a listbox
For i = 0 To UserForm1.ListBox1.ListCount - 1
Gname = UserForm1.ListBox1.List(i) ' get listbox names
' populate temp dictionary
Set dic = FNC.GET_SESSION_FILE_ELEMENTS(mySesFile, Gname)
'
' instantiate new Class Object
Dim NewCol As New cVM_Col
Call NewCol.INIT(dic) ' pass the dictionary to a 'constructor'
dGroup.Add Gname, NewCol.CLONE ' add to the MAIN SUB dictionary
'
Set dic = Nothing ' clear the temp dictionary
Next i
CLASS 对象
Private dElms As Scripting.Dictionary
Private dDat As Scripting.Dictionary
'
Private Sub Class_Initialize()
Set dElms = New Scripting.Dictionary
Set dDat = New Scripting.Dictionary
End Sub
'
Public Sub INIT(inp As Scripting.Dictionary)
Set dElms = inp
End Sub
'
Public Function CLONE()
Set CLONE = New cVM_Col
Set CLONE.dElms = dElms ' <-- THIS IS WHERE IT CRASHES
Set CLONE.dDat = dDat
End Function
通常情况下,当我只克隆简单数据类型(如 String 或 Long 或 Double)时,我的 CLONE 函数会起作用。我从来不需要用字典来做这件事。
要克隆我的 CLASS 对象中的字典对象,我必须进行以下更改:
CLASS 对象 (修改后的 CLONE 函数)
Public Function CLONE()
Set CLONE = New cVM_Col
CLONE.Elms = dElms
CLONE.Dat = dDat
End Function
(添加的属性)
Public Property Get Elms() As Scripting.Dictionary
Set Elms = dElms
End Property
Public Property Let Elms(p As Scripting.Dictionary)
Set dElms = p
End Property
'
Public Property Get Dat() As Scripting.Dictionary
Set Dat = dDat
End Property
Public Property Let Dat(p As Scripting.Dictionary)
Set dDat = p
End Property