VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Using GlobalSingleUse to Avoid Explicit Object Creation

A Global class does not require a client to explicitly instantiate it before the client tries to use it.

In other words, if a Global class exists in a server application that a client is referencing, the client can manipulate the Global class methods and properties as if they were just Public variables and procedures of the client.

Suppose, for example, that your server, named MyServices, has a SingleUse server class named FileSvc, with a property named fsSize and methods named fsFind and fsDelete. If client programmers wanted to use the FileSvc class to manipulate a file, they might write code that looks like Listing 12.13. This code declares and instantiates an object from the server/class combination and then calls the object's methods and properties, using the object.member syntax.


Dim filCurr As New MyServices.FileSvc
filCurr.fsFind "VB6.dep"
If filCurr.fsSize = 0 Then
End If

You could save client programmers some work, however, by setting the Instancing property of the FileSvc class to GlobalSingleUse. If you did so, client programmers could accomplish the same thing by writing the code in Listing 12.14. Notice that it is not necessary to declare or instantiate an object variable, nor is it necessary to use any sort of object reference at all when you want to manipulate the class's properties and methods.

As mentioned in the note accompanying this section, however, you must fully declare the object variable when programming with a GlobalSingleUse class within the component itself.


fsFind "VB6.dep"
If fsSize = 0 Then
End If

Before you decide to make all your externally creatable classes GlobalSingleUse or GlobalMultiUse, however, consider the drawbacks:]

  • Client-side code is now more ambiguous and therefore less maintainable.

  • It's easier to confuse multiple instances of global information. Typically, you will only use GlobalSingleUse when the function a class provides is very, very generic.

Note - Internal Code Must Instantiate GlobalSingleUse Classes Explicitly : Although client projects can access members of GlobalSingleUse classes as if they were Public variables, code within the same server project as the GlobalSingleUse class cannot do so. In other words, if you want to access the members of a GlobalSingleUse class from within the same server project, you must still instantiate an object from the GlobalSingleUse class to do so.

The GlobalSingleUse Instancing setting is not available in ActiveX DLL projects


<< Previous | Contents | Next >>


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved