I added a property so you could see something you could use this for, and I usually add a SyncLock to make the construction Thread Safe. The SyncLock command will only allow one tread into it at a time. I double up the IF statement to avoid hitting the SyncLock after the Object has been created.
Public Class GlobalSettings
Private Shared mySelf As GlobalSettings
Private Sub New()
End Sub
'I added a Property so you could see a use
Public ReadOnly Property CnString() As String
Get
Return "My DB Connection String"
End Get
End Property
Public Shared ReadOnly Property GetInstance() As GlobalSettings
Get
If mySelf Is Nothing Then
SyncLock "Create GlobalSettings"
If mySelf Is Nothing Then
mySelf = New GlobalSettings
End If
End SyncLock
End If
Return mySelf
End Get
End Property
End Class
End Class
Now there are two easy ways to access this object in your code, I kinda like the second one better because you don't have to create another variable.
Public Class MyCode
Public Function DBConnection() As String
Dim dbCon1 As String
Dim dbCon2 As String
Dim gs As GlobalSettings = GlobalSettings.GetInstance
dbCon1 = gs.CnString
dbCon2 = GlobalSettings.GetInstance.CnString
Return dbCon2
End Function
End Class
Comments
There are no comments yet. Be the first to comment!