Click here to Skip to main content
11,645,499 members (70,309 online)
Click here to Skip to main content

Design Patterns using VB.NET

, 30 Sep 2002 91.2K 23
Rate this:
Please Sign up or sign in to vote.
Creating Desing Patterns using VB.NET

Patterns are devices that allow programs to share knowledge about their design. In this article we will try to write implement Singleton patterns using VB.NET. The Singleton pattern applies to many situation where there needs to be only a single instance of the class.

How do we make sure that the class can have only one instance ? One solution is to use a global variable, but it wont stop the client from making multiple instances. The better solutoin would be to make the class intelligent enough to restrict clients from creating more than one instance. This is the Singleton Pattern.

Below is the implementation of a singleton class using VB.NET

        Public Class clsSingleton

            Private Shared objSingle As clsSingleton
            Private Shared blCreated As Boolean
            Public strI As String

            Private Sub New()
                'Override the default constructor
            End Sub

            Public Shared Function getObject() As clsSingleton
                If blCreated = False Then
                    objSingle = New clsSingleton()
                    blCreated = True
                    Return objSingle
                    Return objSingle
                End If
            End Function
        End Class

Let us now examine the above class. In the above class we have made the construtor as private, so that the client cannot create instance of this class using new operator. Function getObject is called to get an instance of the class. Based on the boolean variable blCreated, only during the first time an instance is created. During subsequent calls to getObject no new instance are created but the older one is returned, thus ensuring only one instance of the class is created.

You can test this class by writing clients which can create multiple instance of the above class. By setting values to the string variable in the class you will discover that the class instance is created only once. Now that we have implementation, where can we use this ? There are many instances where microsoft have effectively used singleton pattern like, there exist only one FileSystem in windows,only one window manager etc.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gopi Subramanian
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
pawangupta.gupta96-Feb-10 2:22
memberpawangupta.gupta96-Feb-10 2:22 
QuestionIs this first in a series ? Pin
Christian Graus2-Jun-05 17:30
memberChristian Graus2-Jun-05 17:30 
GeneralPlease change the Heading Pin
RaoSatish2-Jun-05 17:17
memberRaoSatish2-Jun-05 17:17 
Generalok, now how about... Pin
Anonymous12-Dec-03 8:18
sussAnonymous12-Dec-03 8:18 
GeneralBad title Pin
Rince3-Oct-02 10:10
sussRince3-Oct-02 10:10 
The title of this article suggest something more than the article actually provides.
Looking at the promising title one will hardly guess that it is about the trivial and widely spread Singleton pattern.Smile | :)
GeneralMicrosoft's Recommendation Pin
Kevin McFarlane1-Oct-02 1:37
memberKevin McFarlane1-Oct-02 1:37 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 1 Oct 2002
Article Copyright 2002 by Gopi Subramanian
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid