Cancel creation of an object

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I have an object which should be created only once. So I have a Shared
member in there which keeps track of how many instances where created.
Is there a way I could prevent creation of further instances of the
object if 1 already exists?
 
* Max said:
I have an object which should be created only once. So I have a Shared
member in there which keeps track of how many instances where
created. Is there a way I could prevent creation of further instances
of the object if 1 already exists?

Have a Google search on "singleton design pattern dotnet"...
 
Herfried said:
Have a Google search on "singleton design pattern dotnet"...

Thanks, found this implementation and it seems to work nicely:

Public Class clsSingleton

Private Shared objSingle As clsSingleton
Private Shared blCreated As Boolean
Public strI As String

Private Sub New()
'Override the default constructor
End Sub

Public Shared Function getObject() As clsSingleton
If blCreated = False Then
objSingle = New clsSingleton()
blCreated = True
Return objSingle
Else
Return objSingle
End If
End Function
End Class

One more question, if I have this class inside of a dll which could be used by multiple programs, would something like this still be possible or will it count instances for each program separately?
 
Back
Top