Thread: OOPs
View Single Post
  #2 (permalink)  
Old May 17th, 2006, 07:26 AM
spif2001 spif2001 is offline
Authorized User
 
Join Date: Apr 2006
Location: Horsens, , Denmark.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

With the following code:

private static SingletonClass instance = null;
private static object lockObj = new object();

private SingletonClass()
{}//constructor

public static SingletonClass Instance
{
    get
    {
        lock(lockObj)
        {
            if(instance == null)
                instance = new SingletonClass();
            return instance;
        }
    }
}

you can only use the one instance.

To use it simply use the property:
SingletonClass.Instance.WhateverMethod();

The first time you use it, the instance is initialized. All following uses will use the same instance.

Reply With Quote