Click here to Skip to main content
15,880,796 members
Articles / General Programming / Threads
Tip/Trick

Singleton Pattern VB.NET using Static Variable

Rate me:
Please Sign up or sign in to vote.
3.25/5 (4 votes)
24 May 2012CPOL 56.8K   3   15
The most elegant implementation for VB.NET and safe as simple

Introduction

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.

Theory

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

VB.NET
Public Class Singleton

    Private Sub New()

    End Sub

    Public Shared ReadOnly Property GetInstance As Singleton
        Get
            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.

License

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


Written By
Technical Lead
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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
Nicolá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
MPelletierProg9-Apr-14 5:07 
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
Evgeniy Sukhikh5-May-17 6:17
Evgeniy Sukhikh5-May-17 6:17 
SuggestionSingletone Pin
AspDotNetDev25-May-12 13:40
protectorAspDotNetDev25-May-12 13:40 
GeneralRe: Singletone Pin
Evgeniy Sukhikh26-May-12 9:10
Evgeniy Sukhikh26-May-12 9:10 
GeneralIt still needs more explanation Pin
PIEBALDconsult25-May-12 8:39
mvePIEBALDconsult25-May-12 8:39 
GeneralRe: It still needs more explanation Pin
Evgeniy Sukhikh26-May-12 9:32
Evgeniy Sukhikh26-May-12 9:32 
QuestionInstantiation Pin
Ctznkane24-May-12 8:33
Ctznkane24-May-12 8:33 
AnswerRe: Instantiation Pin
PIEBALDconsult25-May-12 8:29
mvePIEBALDconsult25-May-12 8:29 
GeneralMy vote of 1 Pin
fjdiewornncalwe23-May-12 8:45
professionalfjdiewornncalwe23-May-12 8:45 
GeneralRe: My vote of 1 Pin
Evgeniy Sukhikh23-May-12 11:15
Evgeniy Sukhikh23-May-12 11:15 
GeneralRe: My vote of 1 Pin
kfsdavidt6424-May-12 3:25
kfsdavidt6424-May-12 3:25 
GeneralRe: My vote of 1 Pin
Evgeniy Sukhikh24-May-12 22:57
Evgeniy Sukhikh24-May-12 22:57 
QuestionGood, though... Pin
JoseMenendez23-May-12 8:37
JoseMenendez23-May-12 8:37 
AnswerRe: Good, though... Pin
Evgeniy Sukhikh23-May-12 11:16
Evgeniy Sukhikh23-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.