p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   OOPs (http://p2p.wrox.com/showthread.php?t=42673)

bijgupt May 17th, 2006 07:17 AM

OOPs
 
How to create a class of which only single instance be created?

Bijgupt

spif2001 May 17th, 2006 07:26 AM

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.


bijgupt May 17th, 2006 07:49 AM

Thank you so much. I was searching the solution for long time. Now i got what i wanted. Tahnks again.

Bijgupt

spif2001 May 17th, 2006 07:54 AM

My pleasure.


bijgupt May 18th, 2006 04:27 AM

I want to create an object during runtime. The problem is that i want to supply object name at runtime. That is whatever the name user supply, object of that name should be created.How can i do this?

Bijgupt

joefawcett May 18th, 2006 05:36 AM

Please start a new thread for a new question.
Look at the Activator class, you'll need the assembly name and type.

--

Joe (Microsoft MVP - XML)


All times are GMT -4. The time now is 08:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.