I got this code that is almost identical to your code except you have public object between the private classes, you are also missing the Import statement for System.Threading because Mutex manages threads. Check the links below for VB code implementing Singleton
Pattern. The first is your code and the second is the code from the first link. Hope this helps.
Namespace ProjectName.BLL
Public NotInheritable Class WebIServer
Private Shared Instance As WebIServer
Private Shared _mu As New Mutex
Public MyObject As New MyCom.Server
Private Sub New()
MyObject = New MyCom.Server
End Sub
Public Shared Function GetInstance() As WebIServer
_mu.WaitOne()
Try
If Instance Is Nothing Then
Instance = New WebIServer
End If
Finally
_mu.ReleaseMutex()
End Try
Return Instance
End Function
End Class
End Namespace
Imports System.Threading
Namespace Abstractvb.DesignPatterns
Public Class Singleton
Private Shared _singleton As Singleton
Private Shared _mu As New Mutex()
Private Sub New()
End Sub
Public Shared Function GetInstance() As Singleton
_mu.WaitOne()
Try
If _singleton Is Nothing Then
_singleton = New Singleton()
End If
Contributor
4150 Points
5249 Posts
Re: Singleton Design pattern
Jun 03, 2005 01:52 PM|Caddre|LINK
I got this code that is almost identical to your code except you have public object between the private classes, you are also missing the Import statement for System.Threading because Mutex manages threads. Check the links below for VB code implementing Singleton Pattern. The first is your code and the second is the code from the first link. Hope this helps.
Namespace ProjectName.BLL
Public NotInheritable Class WebIServer
Private Shared Instance As WebIServer
Private Shared _mu As New Mutex
Public MyObject As New MyCom.Server
Private Sub New()
MyObject = New MyCom.Server
End Sub
Public Shared Function GetInstance() As WebIServer
_mu.WaitOne()
Try
If Instance Is Nothing Then
Instance = New WebIServer
End If
Finally
_mu.ReleaseMutex()
End Try
Return Instance
End Function
End Class
End Namespace
Imports System.Threading
Namespace Abstractvb.DesignPatterns
Public Class Singleton
Private Shared _singleton As Singleton
Private Shared _mu As New Mutex()
Private Sub New()
End Sub
Public Shared Function GetInstance() As Singleton
_mu.WaitOne()
Try
If _singleton Is Nothing Then
_singleton = New Singleton()
End If
Finally
_mu.ReleaseMutex()
End Try
Return _singleton
End Function
End Class
End Namespace
http://abstractvb.com/code.asp?A=1096
http://www.codeguru.com/columns/VB/article.php/c6563/