As I don't think VBScript language constructions make it natural working with classes, I'd rather use one of the two following aproaches:
1. create the singleton object in Appliction_OnStart event and load it to an Application.Context variable
Create a handler for application-wide OnStart event, then load the single instance of the class and assign it to an Application variable. As this event is the first code to run when the application starts, you can assume the instance will allways be loaded.
The advantage of the aproach is that is not necessary to include any file on pages that will use the sigleton object (as Application.Context is available to all ASP files):
create the singleton instance in Global.asa
Sub Application_OnStart
Set Application.Contents("MySingleton") = new MySingleton
End Sub
use the singleton instance in "anyfile.asp
":
Dim obj : Set instance = Application.Contents("MySingleton")
obj.doAnyMethod()
2. use a function to access the singleton object
Create a function to return the singleton variable. If the obejct is already assigned to an Apllication.Context variable, just return it, if it's not, then load it and assign before returning.
The advantage of this aproach is that the instance is not created until the first time it's used. As I allways have my on library loaded by #include directives, this is no issue to me and would be my prefered way.
create the function to return singleton instance in myLib.inc
Function GetSingleton
If IsEmpty(Application.Contents("MySingleton")) Then
Set Application.Contents("MySingleton") = new MySingleton
End If
Set GetSingleton = Application.Contents("MySingleton")
End Function
use the singleton instance in "anyfile.asp
":
<!--#include virtual="/myLib.inc" -->
Dim obj : Set instance = GetSingleton()
obj.doAnyMethod()