Download code

Jump to: navigation, search

Back to Singleton_pattern_(Visual_Basic_.NET)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

LazySingleton.vb

 1 ' The authors of this work have released all rights to it and placed it
 2 ' in the public domain under the Creative Commons CC0 1.0 waiver
 3 ' (http://creativecommons.org/publicdomain/zero/1.0/).
 4 ' 
 5 ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 ' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 ' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 ' IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 ' CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 ' TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 ' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 ' 
13 ' Retrieved from: http://en.literateprograms.org/Singleton_pattern_(Visual_Basic_.NET)?oldid=6962
14 
15 Module LazySingleton
16     Public Class SingletonObject
17         Private Shared singleInstance As SingletonObject ' Leave set to Nothing
18         Private Sub New()
19 	    ' Private constructor prevents instantiation outside the class
20 	End Sub
21         Public Shared ReadOnly Property Instance() As SingletonObject
22 	    Get
23 	        If singleInstance Is Nothing Then
24 	            singleInstance = New SingletonObject
25 	        End If
26 	        Return singleInstance
27 	    End Get
28 	End Property
29     End Class
30 
31     Sub Main()
32         Dim obj As SingletonObject = SingletonObject.Instance
33         Dim obj2 As SingletonObject = SingletonObject.Instance
34         Console.WriteLine("obj==obj2: " + (obj Is obj2).ToString())
35     End Sub
36 End Module


hijacker
hijacker
hijacker
hijacker

Singleton.vb

 1 ' The authors of this work have released all rights to it and placed it
 2 ' in the public domain under the Creative Commons CC0 1.0 waiver
 3 ' (http://creativecommons.org/publicdomain/zero/1.0/).
 4 ' 
 5 ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 ' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 ' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 ' IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 ' CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 ' TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 ' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 ' 
13 ' Retrieved from: http://en.literateprograms.org/Singleton_pattern_(Visual_Basic_.NET)?oldid=6962
14 
15 Module Singleton
16     Public Class SingletonObject
17         Private Shared singleInstance As SingletonObject = New SingleObject()
18         Private Sub New()
19 	    ' Private constructor prevents instantiation outside the class
20 	End Sub
21         Public Shared ReadOnly Property Instance() As SingletonObject
22 	    Get
23 	        Return singleInstance
24 	    End Get
25 	End Property
26 
27     End Class
28 
29     Sub Main()
30         Dim obj As SingletonObject = SingletonObject.Instance
31         Dim obj2 As SingletonObject = SingletonObject.Instance
32         Console.WriteLine("obj==obj2: " + (obj Is obj2).ToString())
33     End Sub
34 End Module
35 


hijacker
hijacker
hijacker
hijacker