Single-Use versus Multiuse Objects


Multiple instances of an application can be loaded in memory at the same time, but how Visual Basic deals with instances depends on the object's Instancingproperty. All objects created in VB have one of the Instancingsettings shown in Table 23.1.

The Instancingproperty is new in VB5. VB4 used the Creatableand Publicproperties to control instancing in a more limited way. Also changed in VB5 is the GetObjectfunction - this function no longer works with VB-created objects.

Table 23.1 - The New Instancing Property and VB4 Equivalents

Instancingsetting Value Description VB4 Equivalent
Private1 Object is only visible and available within its project. Creatable = False, Public = False
PublicNotCreatable2 Object is visible to other projects, but can only be created within the its project. Creatable = False, Public = True
SingleUse3 Object is visible to and can be created by other projects. Each new CreateObjector Dim As Newcreates a new instance of the object. None
GlobalSingleUse4 Same as SingleUse, but all methods and properties of the object can omit the object name (for example, Resetinstead of objCD.Reset). None
MultiUse5 Object is visible to and can be created by other projects, but only one instance of the object is created and all references share that instance. Creatable = True, Public = True
GlobalMultiUse6 Same as MultiUse, but all methods and properties of the object can omit the object name (for example, Resetinstead of objCD.Reset). None

Single-use objects create a new instance for every new object created with CreateObjector Dim As Newstatements. Multiuse objects use only one instance, regardless of how many times you call CreateObjector Dim As New. These two types of applications pose different problems, as shown in Figure 23.2.

FIG. 23.2

Single-use and multiuse objects pose different sets of problems for programmers.