Software Development

VB6 Tip: Create global properties to gain control over data storage

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:

  1. A Private variable to hold the property value
  2. 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)
  3. 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
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:


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
    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.


hi everyone! i'm working with COM+ and i wonder if it's good idea working with properties in a class on COM+, any suggestions?


I found your post excellent and exactly what I am looking for, however I am trying to implement these constructs now and stumbling over where to put them? If I add them to the Modules I can't see them from other projects in the application. I tried adding them to the Class Modules, but if I do it that way I have to instance the Class to get/let the property and instantiation is what we need to avoid. Can you let me know what part of the VB project you would add this Global Property to allow it to be seen from other projects referencing this one?

Tony Hopkinson
Tony Hopkinson

project based, code module so I presume .bas file. Continental properties. ? An application in visual studio is merely a convenient ay of grouping up code. There is no explicit connection between one project and the other. They could share code but they can't can't share an instance well not directly anyway. You'd have to look at shared memory or out of process to achieve what you want. I'd have a careful look at why you want it before you go down that route though.