Posted By

DaveChild on 09/11/08


Tagged

class Singleton


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Scooter
chavcho


VB.NET Singleton Class


 / Published in: VB.NET
 

URL: http://www.ondotnet.com/pub/a/dotnet/2002/11/11/singleton.html

  1. Public Class ClassName
  2.  
  3. Private Shared objClassName As ClassName
  4. Private Shared blnClassAlreadyCreated As Boolean = False
  5.  
  6. Private Sub New()
  7. End Sub
  8.  
  9. Public Shared Function setupClass() As ClassName
  10. If blnClassAlreadyCreated = False Then
  11. objClassName = New ClassName()
  12. blnClassAlreadyCreated = True
  13. End If
  14. Return objClassName
  15. End Function
  16.  
  17. End Class

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: DaveChild on September 12, 2008

To create a new instance of this object:

Public ClassNameInstance As ClassName = ClassName.setupClass()

Posted By: breakskater on August 28, 2010

Too Complicated...

It's easier to write a singleton class as follows:

Public Class ClassName

  Private Shared _instance As New ClassName 

  Public Shared ReadOnly Property Instance() As ClassName
        Get 
                Return _instance
        End Get
  End Property

You need to login to post a comment.