Click here to Skip to main content
12,897,945 members (84,483 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 23 May 2012

Singleton Pattern VB.NET using Static Variable

, 24 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
The most elegant implementation for VB.NET and safe as simple


I have seen many implementations of the Singleton pattern. This one seems to be the most elegant for VB.NET and is as safe as simple.


Static Local Variables in VB.NET

In VB, a local variable marked as static, apart from usual local variable, continues to exist and retains its most recent value. Moreover, it is initialized only once and this operation is thread safe.

What it simply means is that while the first time property is accessed, the variable is defined and initialized with a value given. Every next call initialization and declaration is ignored and the variable is used as shared/static for this class and continues to exist for a lifetime of the class.

Singleton Pattern Implementation

Public Class Singleton

    Private Sub New()

    End Sub

    Public Shared ReadOnly Property GetInstance As Singleton
            Static Instance As Singleton = New Singleton
            Return Instance
        End Get
    End Property

End Class

Please do let me know if there are any pitfalls or if you have other ideas.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Evgeniy Sukhikh
Web Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...


Comments and Discussions

BugI think that it is wrong because this singleton is always creating a new instance, you need to question if is this nothing Pin
Nicolás Bjorklund14-Nov-13 6:26
memberNicolás Bjorklund14-Nov-13 6:26 
GeneralRe: I think that it is wrong because this singleton is always creating a new instance, you need to question if is this nothing Pin
MPelletierProg9-Apr-14 5:07
memberMPelletierProg9-Apr-14 5:07 
SuggestionSingletone Pin
AspDotNetDev25-May-12 13:40
protectorAspDotNetDev25-May-12 13:40 
GeneralRe: Singletone Pin
EvgeniySu26-May-12 9:10
memberEvgeniySu26-May-12 9:10 
GeneralIt still needs more explanation Pin
PIEBALDconsult25-May-12 8:39
memberPIEBALDconsult25-May-12 8:39 
GeneralRe: It still needs more explanation Pin
EvgeniySu26-May-12 9:32
memberEvgeniySu26-May-12 9:32 
QuestionInstantiation Pin
Ctznkane24-May-12 8:33
memberCtznkane24-May-12 8:33 
AnswerRe: Instantiation Pin
PIEBALDconsult25-May-12 8:29
memberPIEBALDconsult25-May-12 8:29 
GeneralMy vote of 1 Pin
Marcus Kramer23-May-12 8:45
memberMarcus Kramer23-May-12 8:45 
GeneralRe: My vote of 1 Pin
EvgeniySu23-May-12 11:15
memberEvgeniySu23-May-12 11:15 
GeneralRe: My vote of 1 Pin
kfsdavidt6424-May-12 3:25
memberkfsdavidt6424-May-12 3:25 
GeneralRe: My vote of 1 Pin
EvgeniySu24-May-12 22:57
memberEvgeniySu24-May-12 22:57 
QuestionGood, though... Pin
Jose Menendez Póo23-May-12 8:37
memberJose Menendez Póo23-May-12 8:37 
AnswerRe: Good, though... Pin
EvgeniySu23-May-12 11:16
memberEvgeniySu23-May-12 11:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 May 2012
Article Copyright 2012 by Evgeniy Sukhikh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid