给你 一个例子:
==================================================================
窗体部分:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1110
ClientLeft = 60
ClientTop = 345
ClientWidth = 3525
LinkTopic = "Form1"
ScaleHeight = 1110
ScaleWidth = 3525
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdCheat
Caption = "Cheat"
Height = 495
Left = 1920
TabIndex = 1
Top = 240
Width = 1215
End
Begin VB.CommandButton cmdCreateLegally
Caption = "Create Legally"
Height = 495
Left = 360
TabIndex = 0
Top = 240
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub cmdCheat_Click()
Dim obj As SingletonClass Set obj = New SingletonClass
MsgBox TypeName(obj)
End SubPrivate Sub cmdCreateLegally_Click()
Dim obj As SingletonClass Set obj = Singleton
MsgBox TypeName(obj)
End Sub
======================================
模块部分:
Attribute VB_Name = "Module1"
Option ExplicitPrivate m_Singleton As SingletonClass
Private m_SingletonCreationOk As Boolean
' Return a reference to the Singleton.
Public Property Get Singleton() As SingletonClass
' See if it already exists.
If m_Singleton Is Nothing Then
' Flag that this instantiation is ok.
m_SingletonCreationOk = True
Set m_Singleton = New SingletonClass
m_SingletonCreationOk = False
End If Set Singleton = m_Singleton
End Property' Indicate whether it is ok to create the
' Singleton now.
Public Function SingletonCreationOk() As Boolean
SingletonCreationOk = m_SingletonCreationOk
End Function
=====================================
类:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "SingletonClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit' Make sure it is ok to create the Singleton.
Private Sub Class_Initialize()
If Not SingletonCreationOk Then
Err.Raise vbObjectError + 1001, _
"SingletonClass", _
"Illegal SingletonClass instantiation"
End If
End Sub
==================================================================
窗体部分:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1110
ClientLeft = 60
ClientTop = 345
ClientWidth = 3525
LinkTopic = "Form1"
ScaleHeight = 1110
ScaleWidth = 3525
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdCheat
Caption = "Cheat"
Height = 495
Left = 1920
TabIndex = 1
Top = 240
Width = 1215
End
Begin VB.CommandButton cmdCreateLegally
Caption = "Create Legally"
Height = 495
Left = 360
TabIndex = 0
Top = 240
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub cmdCheat_Click()
Dim obj As SingletonClass Set obj = New SingletonClass
MsgBox TypeName(obj)
End SubPrivate Sub cmdCreateLegally_Click()
Dim obj As SingletonClass Set obj = Singleton
MsgBox TypeName(obj)
End Sub
======================================
模块部分:
Attribute VB_Name = "Module1"
Option ExplicitPrivate m_Singleton As SingletonClass
Private m_SingletonCreationOk As Boolean
' Return a reference to the Singleton.
Public Property Get Singleton() As SingletonClass
' See if it already exists.
If m_Singleton Is Nothing Then
' Flag that this instantiation is ok.
m_SingletonCreationOk = True
Set m_Singleton = New SingletonClass
m_SingletonCreationOk = False
End If Set Singleton = m_Singleton
End Property' Indicate whether it is ok to create the
' Singleton now.
Public Function SingletonCreationOk() As Boolean
SingletonCreationOk = m_SingletonCreationOk
End Function
=====================================
类:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "SingletonClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit' Make sure it is ok to create the Singleton.
Private Sub Class_Initialize()
If Not SingletonCreationOk Then
Err.Raise vbObjectError + 1001, _
"SingletonClass", _
"Illegal SingletonClass instantiation"
End If
End Sub
但 还是使用了 全局变量的。