Program to write a Singleton Pattern using Nested Class in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 436
The below program will create a Singleton Pattern using Nested Class concept in VB.net

Namespace ConsoleApplication1
Public NotInheritable Class Singleton
Private Shared m_instance As Singleton = Nothing
Private Shared ReadOnly lockObj As New Object()

Private Sub New()
End Sub
'private constructor
Public Shared ReadOnly Property Instance() As Singleton
Get
SyncLock lockObj
If System.[Object].ReferenceEquals(m_instance, Nothing) Then
m_instance = Inner.Instance
End If
Return m_instance
End SyncLock
End Get
End Property

Private Class Inner
Private Sub New()
End Sub
'private constructor
Public Shared ReadOnly Property Instance() As Singleton
Get
Return New Singleton()
End Get
End Property
End Class
End Class
End Namespace

Comments or Responses

Login to post response