Jun
26
2003

PropertyBag Class

Persist private objects with the PropertyBag Class

If you use the PropertyBag Object then you'll know how indispensable it is. It does have some drawbacks though (primarily the lack of persistence for private object modules). My PropertyBag Class goes a long way towards fixing those drawbacks. I’ve added the ability to persist private object modules, to output to file (binary) or string (base64 encoded), to optionally compress the output using the zlib library, and I’ve managed to keep it compatible with the Microsoft’s PropertyBag!

I must give mention to Francesco Balena (see the links at the bottom of the page) whose article on the PropertyBag inspired me.

What it can do

  • Retains compatibility with Visual Basic's built-in PropertyBag
  • Persist Private Class Modules using the IPropertyBagClass Interface
  • Save/Load the PropertyBagClass contents to file or string
  • Optionally compress the PropertyBagClass Contents using the ZLib dll

Usage

Add PropertyBagClass.cls and IPropertyBagClass.cls to your Project

'To persist a Private Object you need to Implement the IPropertyBag Class
'MyGameClass Object
Option Explicit

Implements IPropertyBagClass

Public Name As String
Public Score As Long
Public HitPoints As Long
Public Lives As Long

Public PosX As Long
Public PosY As Long

Private Sub IPropertyBagClass_ReadProperties(PropBag As PropertyBagClass)

   Name = PropBag.ReadProperty("Name", "")
   Score = PropBag.ReadProperty("Score", 0)
   HitPoints = PropBag.ReadProperty("HitPoints", 0)
   Lives = PropBag.ReadProperty("Lives", 0)

   PosX = PropBag.ReadProperty("PosX", 0)
   PosY = PropBag.ReadProperty("PosY", 0)

End Sub

Private Sub IPropertyBagClass_WriteProperties(PropBag As PropertyBagClass)

   PropBag.WriteProperty "Name", Name
   PropBag.WriteProperty "Score", Score
   PropBag.WriteProperty "HitPoints", HitPoints
   PropBag.WriteProperty "Lives", Lives

   PropBag.WriteProperty "PosX", PosX
   PropBag.WriteProperty "PosY", PosY

End Sub

'And to save it...

   Dim mg As MyGameClass
   Dim pb As PropertyBagClass

   Set pb = New PropertyBagClass
   pb.WriteProperty "MyGame", MyGame
   If Not pb.SaveToFile(Path, False, True) Then
      MsgBox "Couldn't save the file " & Path, vbExclamation
   End If
   Set pb = Nothing

'And to reload it...

   Dim mg As MyGameClass
   Dim pb As PropertyBagClass

   Set pb = New PropertyBagClass
   If pb.LoadFromFile(Path) Then
      Set mg = New MyGameClass
      'Note: When reading a private object you have to create an instance
      'of it first and pass it as the default parameter
      Set mg = pb.ReadProperty("MyGame", mg)
      Set MyGame = mg
      Set mg = Nothing
   Else
      MsgBox "Couldn't load the file " & Path, vbExclamation
   End If
   Set pb = Nothing

Downloads

  PropertyBagClass.zip - contains: PropertyBagClass.cls, IPropertyBagClass.cls, zlib.dll (29.5 kb)

  MyGame.zip - A simple project to demonstarte how to persist private objects (27.4 kb)

Links

Another good article on vbaccelerator about persisting objects/data to XML by Steve McMahon

More...