professional web development and design
 

Creating a Thread Safe Singleton in VB.NET - Part 2


Here is the static method that will return our one and only instance of our object:
Public Shared Function GetInstance() As IMortgageProDAC
	SyncLock objectLock
		If instance Is Nothing Then
			instance = New ApplicationDAC
		End If
		Return instance
	End SyncLock
End Function
The "SyncLock" keywork is similar to C#'s lock() mechanism and will use the static object that we declared to ensure only one thread at a time will access our instance.

If the static instance variable is null, this method will create and instance of the class, else it will simply return the existing instance persisted in memory.

VB.NET Singleton back

Valid HTML 4.01