Download code

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 THE11' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12' 13' Retrieved from: http://en.literateprograms.org/Singleton_pattern_(Visual_Basic_.NET)?oldid=69621415Module LazySingleton
16PublicClass SingletonObject
17PrivateShared singleInstance As SingletonObject ' Leave set to Nothing18PrivateSubNew()19' Private constructor prevents instantiation outside the class20EndSub21PublicSharedReadOnlyProperty Instance()As SingletonObject
22Get23If singleInstance IsNothingThen24 singleInstance = New SingletonObject
25EndIf26Return singleInstance
27EndGet28EndProperty29EndClass3031Sub Main()32Dim obj As SingletonObject = SingletonObject.Instance
33Dim obj2 As SingletonObject = SingletonObject.Instance
34 Console.WriteLine("obj==obj2: " + (obj Is obj2).ToString())35EndSub36EndModule

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 THE11' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12' 13' Retrieved from: http://en.literateprograms.org/Singleton_pattern_(Visual_Basic_.NET)?oldid=69621415Module Singleton
16PublicClass SingletonObject
17PrivateShared singleInstance As SingletonObject = New SingleObject()18PrivateSubNew()19' Private constructor prevents instantiation outside the class20EndSub21PublicSharedReadOnlyProperty Instance()As SingletonObject
22Get23Return singleInstance
24EndGet25EndProperty2627EndClass2829Sub Main()30Dim obj As SingletonObject = SingletonObject.Instance
31Dim obj2 As SingletonObject = SingletonObject.Instance
32 Console.WriteLine("obj==obj2: " + (obj Is obj2).ToString())33EndSub34EndModule35