C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Singleton design pattern for c# application


Posted Date: 14 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Anand_lvMember Level: Silver    
Rating: 1 out of 5Points: 7



SINGLETON IS A DESIGN PATTERN USED IN C# APPLICATIONS ...
IN MANY APPLICATION WE ACTUALLY CREATING CLASSES AND METHODS FOR OOPS CONCEPTS.

THINK THAT WE ARE HAVING AN APPLICATION,IN THAT APPLICATION WE ARE CREATING CLASS1
AND CALLING IN ALL OUR FORMS .
IN ALL FORMS WE HAVE TO CREATE A CLASS OBJECTS TO PREVENT THIS WE HAVE TO IMPLEMENT SINGLETON.
IN SINGLETON IT CHECKS WHETHER CLASS ALREADY HAVE OBJECT OR NOT.IF IT HAS,THEN SECOND OBJECTS WON'T GET CREATED.
TO BE CLEAR TRY THIS

NOTE:WHEN U DEBUG THIS CODE.U CAN UNDERSTAND,HOW THE OBJECTS ARE CREATED AND HANDLED BY SINGLETON.

public class CodSingleTon
{
private static CodSingleTon codInstance;
private decimal _Result=0;
private CodSingleTon()
{
}


public static CodSingleTon Instance
{
get
{
if (codInstance == null)
{
codInstance = new CodSingleTon();
}
return codInstance;

}
}
public void AddNumbers(decimal _Value)
{
_Result = _Value;
}

public decimal GetResult()
{
return _Result;
}
}
private void button1_Click(object sender, EventArgs e)
{


CodSingleTon cls = CodSingleTon.Instance;
CodSingleTon cls1 = CodSingleTon.Instance;
cls.AddNumbers(Convert.ToDecimal(txtInput.Text));

MessageBox.Show(cls.GetResult().ToString());

cls1.AddNumbers(Convert.ToDecimal(txtInput.Text));
MessageBox.Show(cls1.GetResult().ToString());


}




Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Singleton design patterns  .  

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: Storing and Retrieving Image in SQL Server
Previous Resource: using security.principle and environment namespace
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use