Visual Basic

Depersisting an object

Once our Batsman is contained inside a text file (or any other type of storage), it is easy to send him wherever we want. We could take the FIRSTBAT.TXT file and send it with perhaps an entire cricket scorecard to our newspaper's office for incorporating into a newspaper report of the day's play. The code to reuse the Batsman object would look something like this:

Private pb As PropertyBag     ' Declare a PropertyBag object.
  Private oBatsman As Batsman   ' Declare a Batsman object.
  Private Sub Form_Load()
      Dim varTemp As Variant
      Dim byteArr() as Byte
      ' Instantiate the PropertyBag object.
      Set pb = New PropertyBag
      ' Read the file contents into a Variant.
      Open " C:\tms\FirstBat.txt " For Binary As #1
      Get #1, , varTemp
      Close #1
      ' Assign the Variant to a Byte array.
      ByteArr = varTemp
      ' Assign to the PropertyBag Contents property.
      pb.Contents = ByteArr
      ' Instantiate the object from the PropertyBag
      Set oBatsman = pb.ReadProperty("FirstManIn")
  End If

It isn't the same object being created in one place and reused in another; it is an exact copy of the object. This ability to "clone," or copy, an object for reuse offers a lot of potential.

Componentizing All of This

The preceding pages have tried to identify the knowledge that a good data-access component might encapsulate. The next thing to do is design the component's programming model interface. That is a job for another day, and it has been done better than I could have in the excellent article "The Basics of Programming Model Design," by Dave Stearns of Microsoft.


An area I have not seen fit to explore here is that of using the enhanced data binding in Visual Basic 6 to bind directly to middle-tier data access components. However, there is an early experiment with this in the subdirectory TierDataBind in the samples directory for this chapter.