Storing And Extracting Data From A Property Bag
To
do anything involving a property bag, you need to first create a
property bag object. The following code creates a new instance of a
property bag, with the name objBag.
- Dim objBag As New PropertyBag
Not hard, huh? The property bag is part of the Visual Basic IDE so you don't even need a reference to it.
As with all instances of object, always remember to destroy the object, once you're done with it.
Doing
so prevents those nasty memory leaks. But we don't want to destroy our
property bag object just yet - we need to use it first!
Storing data in our property bag would be done like so:
- objBag.WriteProperty [Name ID], [Value], [Default Value]
- ' Example:
- objBag.WriteProperty "Str", "A string"
Here
we're storing the string "A string" in the property bag using the
string "Str" to tag it. We use that tag when we will extract data from
the property bag. Here is how to read the written value.
- objBag.ReadProperty [Name ID], [Default Value]
- ' Example:
- Dim Str as String
- Str = objBag.ReadProperty "Str", "There is no value stored in the property bag"
Notice
that you would store the property bag data in incompatible varible type
without getting a compile-error, but first getting an error at run-time.