Articles → .NET DESIGN PATTERN → Singleton Design Pattern

Singleton Design Pattern



















public partial class Form1 : Form
{
    static Form1 myInstance;    
    
    private Form1()
    {    
        InitializeComponent();        
    }
    
    public static Form1 GetInstance()
    {    
        if (myInstance == null)
        {        
            myInstance = new Form1();            
        }        
        return myInstance;        
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {    
        e.Cancel = true;        
        this.Hide();         
    } 
}










public partial class Form2 : Form
{
    public Form2()
    {    
        InitializeComponent();        
    }

    private void button1_Click(object sender, EventArgs e)
    {   
    
        Form1 t = Form1.GetInstance(); // creating the instance of singleton class.        
        t.Show();                 
    }
}









Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, October 10, 2010

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250