Storing collections in property bags

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.