This article originally appeared in the Visual Basic newsletter. Click here to automatically subscribe to our free Visual Basic newsletter.
Most programmers know that properties are the primary method by which objects store data and make it available to the program. In Visual Basic, you add properties to a class definition by using Property Get and Property Let procedures. But relatively few programmers know that you can also create global properties that aren't associated with any particular object but are available throughout the entire application.
You may wonder why you would want global properties when you could just create a global variable. The answer lies in the way properties work. You access a global variable directly, like any other variable. In contrast, you access a property indirectly via its Property Let and Property Get procedures. This allows you to write additional code that executes whenever the property is set or read. You can use this code for data validation or any other task that the program may require. This isn't possible if you're using a global variable.
How it works
There are three requirements to creating a global property in a code module:
- A Private variable to hold the property value
- A Property Let procedure for setting the property value (if the variable will hold an object reference, you will use a Property Set procedure instead)
- A Property Get procedure for retrieving the property value
The Get and Let procedures must have the same name. This is the property name that you will use in the program.
The following is sample code for a global property that holds a string value:Private name As String
Public Property Let gName(ByValnewvalue As String)
name = newvalue
Public Property Get gName() As String
gName = name
Here's how it works. Suppose that code elsewhere in the program sets the property as follows:gName = "Alice"
This automatically calls the Property Let procedure with the new value (which is "Alice" in this case) passed as its argument. The code in the Property Let procedure assigns this value to the associated Private variable (which is name in this example). When code reads the value of the property, for example:MsgBox(gName)
It calls the Property Get procedure and returns the value of the Private variable name to the program. (Clearly, the Get procedure is really a function).
In this example, there is no extra processing, so the property behaves just like a global variable. You could, however, include value checking. For example, you might do so to store a default name if the program tries to store a blank name. That would require the following change to the Property Set procedure:Public Property Let gName(ByValnewvalue As String)
If newvalue = "" Then
name = "Default name"
name = newvalue
The use of global properties provides you with more control over global data storage than is possible with global variables.
Peter Aitken has been programming with Visual Basic since Version 1.0. He has written numerous books and magazine articles on Visual Basic and other computer and programming topics.