Post

 Resources 

Console

Custom Resource Files Using Property Bags


Saving And Reading A Property Bag From Disk



Storing data in a property bag internally at runtime could probably be useful on some occations, but what we're interested in is saving the data while the application is closed. Just like the .frx-files store the forms non-text contents when the project isn't opened.

Saving a property bag to disk can be done as the following method demonstrates:

  1. Private Sub SaveContents(Contents As Variant, FilePath As String)
  2.   Dim FileNum As Integer
  3.     FileNum = FileSystem.FreeFile()
  4.     Open FilePath For Binary As FileNum
  5.         Put #FileNum, , Contents
  6.     Close FileNum
  7. End Sub

The method would be called in the following manner:

  1. Dim objBag As New PropertyBag
  2.     With objBag
  3.     .WriteProperty "Str", "A string"
  4.     SaveContents .Contents, App.Path & "Things.bag"
  5. End With
  6. Set objBag = Nothing


Where objBag.Contents is all the data that the property bag stores, all packed into one messy Variant-variable.

Now we're got the property bag saved to disk - great! But we still need to be able to read it again, and here's how to do that:

  1. Private Function LoadContents(FilePath As String) As Variant
  2.   Dim FileNum As Integer
  3.   Dim tempContents As Variant
  4.     FileNum = FileSystem.FreeFile()
  5.     Open FilePath For Binary As FileNum
  6.         Get #FileNum, , tempContents
  7.     Close FileNum
  8.     LoadContents = tempContents
  9. End Function

The method would be called in the following manner:

  1.     Dim objBag As New PropertyBag
  2.     With objBag
  3.         .Contents = LoadContents(App.Path & "Things.bag")
  4.         Dim Str as String
  5.         Str = objBag.ReadProperty "Str", "There is no value stored in the property bag"
  6.     End With
  7.     
  8.     Set objBag = Nothing

That's really all there is to it!

You can, as mentioned, also store pictures, fonts, etc.. This without even changing the way you assign the data to the propertybag. Normally you would use Set [Object] = [Object], but you don't have to, because it of the way it's stored, so the object-files are just handled as Variant-type data.

But to extract object-data, such as picture-files, you need to use the Set ... =-syntax. Examine the example project to see how this is done.




Next: Conclusion | 1 | 2 | 3 | 4 | 5

1 comment

Tutorial Console

Tutorial by:

Anders Nissen


Date: 2004 Oct 11


Navigate



1 comment

Latest comment


by: Eric Coleman

Very nice tutorial! I always thought the property bag was for custom controls only. This looks like a nice replacement for .ini files.

Post a Comment


Printer Friendly



Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 11 visitors within the last 20 minutes
RSS News Feed