C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Creating a Singleton Object


Posted Date: 28 Apr 2006    Resource Type: Articles    Category: .NET Framework

Posted By: phanindra yerra       Member Level: Gold
Rating:     Points: 12



Introduction


Sometimes we need a class that has to be instantiated only once. In such cases singleton classes are used. Singleton object is not a new thing and is a part of oops paradigm.

Implementation



Typically every class is instantiated using constructors. Suppose if you make the access modifier of constructor to private, you wont be able to access the constructor outside the class. This means you cannot instantiate the class.

To access the public members of such a class now shifts towards the impossible. But, this can be done by having static methods inside the class. Static methods of a class are used as : classname.Methodname ()

The static method we are trying to create should return it's class's type. This way may are restricting our class to single instanced class.

Example



This sample code gives some clarification.


public class MySingletonClass
{
//////// declarations

private string _name;
private string _company;
private static MySingletonClass singletonobject = new MySingletonClass();

//////// here some properties
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public string Company
{
get
{
return _company;
}
set
{
_company = value;
}
}



///// here private constructor

private MySingleTonClass()
{
_name = “Phani”;
_company = “KST”;
}



public static MySingletonClass GetSingletonObject()
{
return singletonobject;

}

}

Now, you create singleton object in your application as follows:

  
MySingletonObject singleton = MySingletonObject.GetSingletonObject();
singleton.Name = "Phanindra”;
singleton.Company = “KST”;


Summary



This article describes what singleton object is and how to create one and these Singleton objects are mainly used as session objects. Just directly assign this reference to


System.Web.HttpContext.Current.Session[“SingletonSession”] = singleton;





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Singleton object  .  Singleton classes  .  Singleton class  .  Creating singleton objects  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Object Oriented Programming In c#
Previous Resource: How to find the windows folder and other special folders.
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use