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 singleton class in C#


Posted Date: 02 Mar 2007    Resource Type: Code Snippets    Category: C# Syntax

Posted By: Md Zafar Imam       Member Level: Gold
Rating:     Points: 10



Singleton class is one from which we wan instantiate only one object. For creating singleton class, we have to make the constructor as private and provide a special method to instantiate the class. In this code sample, the method GetProgram() will create the instance if it does nto exist and return the instance.



public class Program
{
private static Program m_Program = null;
private frmInputForm m_inputForm;

private Program()
{
m_inputForm = new frmInputForm();
}

public static Program GetProgram()
{
if (m_Program == null)
{
m_Program = new Program();
}
return m_Program;
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(GetProgram().GetInputForm());
}
public frmInputForm GetInputForm()
{
return m_inputForm;
}
}




Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: How to use the 'using' statement to dispose the object automatically
Previous Resource: Rounding numbers in C#
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween masks

Contact Us    Privacy Policy    Terms Of Use