类对象中嵌套字典的VBA代码

类对象中嵌套字典的VBA代码,vba,class,object,dictionary,nested,Vba,Class,Object,Dictionary,Nested,我试图使用类对象将变量数据存储在字典中(也存储在类对象中)。字典有子字典来存储类对象中的嵌套数据。当我加载新数据时,字典中的值不断被覆盖。请帮忙 以下是主要节目: Sub TReeTestShort() Dim RootVar As cTree Dim ClassLoader As cTree Dim Key As Variant Set RootVar = New cTree RootVar.InitDictOnly With RootVar Set ClassLoader = Ne

我试图使用类对象将变量数据存储在字典中(也存储在类对象中)。字典有子字典来存储类对象中的嵌套数据。当我加载新数据时,字典中的值不断被覆盖。请帮忙

以下是主要节目:

Sub TReeTestShort()
Dim RootVar As cTree
Dim ClassLoader As cTree
Dim Key As Variant

Set RootVar = New cTree
RootVar.InitDictOnly
With RootVar
    Set ClassLoader = New cTree
    .Dict.Add 1, ClassLoader.InitBranch("2008/02/02")
    .Dict.Add 2, ClassLoader.InitBranch("2008/03/03")
    .Dict.Add 3, ClassLoader.InitBranch("2008/04/04")
    Debug.Print RootVar.Dict(1).Branch ' Prints 2008/04/04 instead of 2008/02/02
    With RootVar.Dict(1)
        Set ClassLoader = New cTree
        .Dict.Add 1, ClassLoader.InitLeave("SOL")
        Debug.Print .Dict(1).Leave
            With RootVar.Dict(1).Dict(1)
                Set ClassLoader = New cTree
                .Dict.Add 1, ClassLoader.InitBranch("EY50")
                Debug.Print .Dict(1).Branch
            End With
    End With
End With

For Each Key In RootVar.Dict()
    Debug.Print RootVar.Dict(Key).Branch
Next Key

End Sub
下面是类对象(称为cTree)


在使用类对象加载新字典项之前,您需要重置类对象,方法是添加:

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)


携程在哪里?我认为问题可能出在cTree中。@Don-我附加了错误的类对象。这里是cTree:
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
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