我试图使用类对象将可变数据存储在字典中(也在类对象中)。字典具有子字典以存储来自类对象的嵌套数据。当我加载新数据时,我的字典中的值不断被覆盖。请协助。
这是主程序:
Option Explicit
Private pBranch As String
Private pLeave As String
Private pChildren As Collection
Private pDict As Dictionary
Public Property Get Branch() As String
Branch = pBranch
End Property
Public Property Get Leave() As String
Leave = pLeave
End Property
Public Property Get Children() As Collection
Set Children = pChildren
End Property
Public Property Get Dict() As Dictionary
Set Dict = pDict
End Property
Public Property Let Branch(BValue As String)
pBranch = BValue
End Property
Public Function Init(BValue As String, LValue As String) As cTree
pLeave = LValue
pBranch = BValue
Set pChildren = New Collection
Set Init = Me
End Function
Public Function InitDictOnly() As cTree
Set pDict = New Dictionary
Set InitDictOnly = Me
End Function
Public Function InitBranch(BValue As String) As cTree
pBranch = BValue
Set pDict = New Dictionary
Set InitBranch = Me
End Function
Public Function InitLeave(LValue As String) As cTree
pLeave = LValue
Set pDict = New Dictionary
Set InitLeave = Me
End Function
这是类对象(称为cTree)
.border1 {
border: 1px solid #66FFFF;
}
.border2 {
border: 1px solid #33CCFF;
}
.border3 {
border: 1px solid #0099FF;
}
答案 0 :(得分:1)
您需要先重置类对象,然后再使用它来加载新的字典项:
Set ClassLoader = New cTree
每个
之前.Dict.Add 1, ClassLoader.InitBranch("2008/02/02")
以下是更新的子目录:
Sub TReeTestShort()
Dim RootVar As cTree2
Dim ClassLoader As cTree2
Dim Key As Variant
Set RootVar = New cTree2
With RootVar
.Init "", ""
Set ClassLoader = New cTree2
.Dict.Add "key1", ClassLoader.Init("2008/02/02", "")
Set ClassLoader = New cTree2
.Dict.Add "key2", ClassLoader.Init("2008/03/03", "")
Set ClassLoader = New cTree2
.Dict.Add "key3", ClassLoader.Init("2008/04/04", "")
For Each Key In RootVar.Dict()
Debug.Print RootVar.Dict.Item(Key).Branch
Next Key
Set ClassLoader = New cTree2
.Dict("key1").Dict.Add "key1.1", ClassLoader.Init("", "SOL")
Debug.Print RootVar.Dict("key1").Dict.Item("key1.1").Leave
Set ClassLoader = New cTree2
.Dict("key1").Dict("key1.1").Dict.Add "key1.1.1", ClassLoader.Init("EY50", "")
Debug.Print RootVar.Dict("key1").Dict("key1.1").Dict("key1.1.1").Branch
End With
End Sub
下面是更新的类对象(称为cTree2)
Option Explicit
Private pBranch As String
Private pLeave As String
Private pDict As Dictionary
Public Property Get Branch() As String
Branch = pBranch
End Property
Public Property Get Leave() As String
Leave = pLeave
End Property
Public Property Get Dict() As Dictionary
Set Dict = pDict
End Property
Public Function Init(BValue As String, LValue As String) As cTree2
pLeave = LValue
pBranch = BValue
Set pDict = New Dictionary
Set Init = Me
End Function