Have you ever wondered where the icons, pictures, cursors etc. from your forms are stored when Visual Basic isn't open? They are stored in form's .frx-file. All the infomation is stored in binary format using a method called property bags, and you can also use this extremely simple interface to store and extract multiple sources of data.
Text, numbers, boolean values, icons, pictures, fonts - you name it, it can handle them all! It's almost too simple, just read ahead.
The Property Bag
If you've ever made a user control, you will probably know what a property bag is, because that's where it's used the most.
Basically a property bag is a virtual container that can store almost any type of value. All information in the property bag is stored in Visual Basic's Variant - format. This allows for the user, that would be you, to add data to the property bag, and not worry about what kind of data it is. Be warned though, because the Variant - format can store any kind of data, you must carefully handle how you read the value that you've stored. You don't want to accidentally try and squeeze a picture-file into a Integer - variable.
The Variant - format is also the slowest variable-type due to its non-restrictive nature, but in most cases we only need to extract or store the values a few times during the programs execution so that won't be a problem.
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.
The property bag is part of the Visual Basic (VB) IDE (Integrated Development Environment) 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:
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.
Notice that you would store the property bag data in incompatible variable type without getting a compile-error, but first getting an error at run-time.