VB6 Tip: Create global properties to gain control over data storage
Takeaway: Learn how to create global properties that aren't associated with any particular object but are available throughout the entire application.
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 StringPublic Property Let gName(ByValnewvalue As String)
name = newvalue
End Property
Public Property Get gName() As String
gName = name
End Property
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"
Else
name = newvalue
End If
End Property
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.
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- Next Generation Mobility Now Sprint
- Advances in Data Warehouse Performance: I/O Elimination in DB2 IBM
- Live Webcast: Top Ten Challenges with On-Premise Email Management Dell MessageOne
- IBM pureXML for SOA: Unlocking the business value of information IBM
- Case Study: Clackamas County Oregon's Outdated Fibre Channel Infrastructure Runs Out of Capacity Dell EqualLogic
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





