Storing collections in property bags

From HashVB
Revision as of 00:51, 13 June 2006 by Dee (Talk | contribs)

Jump to: navigation, search
 This article is currently work in progress. Please come back later.

This is a very brief sample of how to store collections of objects in a property bag. This is most of use in custom controls.

In the control itself:

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  Set IntTabs = PropBag.ReadProperty("Tabs", IntTabs)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  PropBag.WriteProperty "Tabs", IntTabs
End Sub

Seeing as I create wrapper classes for my collections, heres what the wrapper does when told to "store" itself:

Private Sub Class_ReadProperties(PropBag As PropertyBag)
Dim Count As Long
Dim Index As Long
Dim NewTab As CCTabStrip.Tab

  Count = PropBag.ReadProperty("TabCount", 0)
  For Index = 1 To Count
    Set NewTab = PropBag.ReadProperty("Tab" & CStr(Index))
    IntTabs.Add NewTab
  Next
End Sub

Private Sub Class_WriteProperties(PropBag As PropertyBag)
Dim Index As Long

  PropBag.WriteProperty "TabCount", IntTabs.Count
  For Index = 1 To IntTabs.Count
    PropBag.WriteProperty "Tab" & CStr(Index), IntTabs(Index)
  Next
End Sub

I can then rely on each Tab object to write out its own stuff:

Private Sub Class_ReadProperties(PropBag As PropertyBag)
  Caption = PropBag.ReadProperty("Caption", "")
  Set Image = PropBag.ReadProperty("Image", Nothing)
End Sub

Private Sub Class_WriteProperties(PropBag As PropertyBag)
  PropBag.WriteProperty "Caption", Caption
  PropBag.WriteProperty "Image", Image
End Sub

Another approach is to write each property of all the sub classes in the control. Unfortunately, this leads to hideous naming schemes. Using the method described above, each object is self contained.

When this propertybag is finally stored in your form file, it will appear as:

Begin CCTabStrip.TabStrip TabStrip1 
   Height          =   2055
   Left            =   240
   TabIndex        =   3
   Top             =   240
   Width           =   4095
   _ExtentX        =   7223
   _ExtentY        =   3625
   BeginProperty Tabs {14A86EAB-1F15-4CAF-8047-A847E3230E70} 
      TabCount        =   2
      BeginProperty Tab1 {C9FCBE2F-471D-4B4F-8A37-9332B4FC63E5} 
         Caption         =   "Tab strip"
      EndProperty
      BeginProperty Tab2 {C9FCBE2F-471D-4B4F-8A37-9332B4FC63E5} 
         Caption         =   "New tab"
      EndProperty
   EndProperty
End