title: VBA code for nested dictionaries in class objects
body: <p>I'm trying to use a class object to store variable data in a dictionary (also in the class object). The dictionary has sub dictionaries to store nested data from the class object. The values in my dictionary keeps getting overwritten as I load new data. Please assist.</p>
  
  <p>Here is the main program:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is the class object (called cTree)</p>
  
  <pre><code>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
  </code></pre>