Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / Visual Basic
Article

Singleton

Rate me:
Please Sign up or sign in to vote.
2.00/5 (3 votes)
11 Oct 2013CPOL1 min read 8.2K   4  
Singleton The Singleton Design Pattern ensures that only a single instance of a given object can exist.It does this by making the class

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Singleton 

The Singleton Design Pattern ensures that only a single instance of a given object can exist.

It does this by making the class constructor private so that it [the singleton itself] has full control over when the class instance is created.  In order to gain access to an instance of a class that implements the Singleton pattern, the developer must call a shared/static method of that Singleton class.

A VB example of a Singleton

Public Class SingletonSample

 

    'shared members

    Private Shared _instance As New SingletonSample

 

    Public Shared Function Instance() As SingletonSample

        Return _instance

    End Function

 

    'instance members

    Private Sub New()

        'public instantiation disallowed

    End Sub

 

    'other instance members

    '...

 

End Class

A C# example of a Singleton

public class SingletonSample

{

    //shared members

    private static SingletonSample _instance = new SingletonSample();

 

    public static SingletonSample Instance()

    {

        return _instance;

    }

 

    //instance members

    private SingletonSample()

    {

        //public instantiation disallowed

    }

 

    //other instance members

    //...

}

 

With the Singleton Design Pattern in place, the developer can easily access that single object instance without needing to worry about inadvertently creating multiple instances, and provides a global point of access to it.

VB - Dim mySingleton As SingletonSample = SingletonSample.Instance

C# - SingletonSample mySingleton = SingletonSample.Instance();

This article was originally posted at http://wiki.asp.net/page.aspx/288/singleton

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --