Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Even after googling I didn't find anything exactly fitting!

Please help me in this.

Modification:

Can somebody please explain this line in the Singleton pattern:

C#
public class OnlyOne
{
    // Please explain this line too me
    private static OnlyOne instance = null;

    private OnlyOne()
    {
    }

    public static OnlyOne getInstance()
    {
        if(instance==null)
        {
            instance = new OnlyOne();
        }
        return instance;
    }
}
Posted
Updated 15-Mar-11 0:03am
v6
Comments
Manfred Rudolf Bihy 15-Mar-11 5:51am    
Please see my adapted solution. I put a hint in there that will help you.
Tarun.K.S 15-Mar-11 5:53am    
The constructor is private, so you cannot create an object of OnlyOne class. There is a getInstance method which you have to call to return the instance. Note that it will be created only once.
Manfred Rudolf Bihy 15-Mar-11 6:01am    
Edit: Spelling and grammar. Tagged correctly. Added question extension OP had posted as a solution.
Albin Abel 15-Mar-11 14:50pm    
I think OP can accept all of the answers below

That's possible using a Singleton Class.
Check this link :

http://csharpindepth.com/Articles/General/Singleton.aspx[^]

The author provided a nice and elegant explanation to implement it, its thread safe too.

Hope it helped! :)
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 15-Mar-11 5:40am    
Singleton is the way to go! 5+
Tarun.K.S 15-Mar-11 5:49am    
Thanks! :)
Dalek Dave 15-Mar-11 5:45am    
Good Link
Tarun.K.S 15-Mar-11 5:50am    
Thanks :)
Albin Abel 15-Mar-11 13:24pm    
Thread safe is a good measure for web applications though it has little over head. My 5
Okay. Did you come across class the design pattern called Singleton class yet?

Here:
http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]
http://msdn.microsoft.com/en-us/library/ee817670.aspx[^]
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 15-Mar-11 5:39am    
If you don't mind I changed the first "class" to design pattern.
Otherwise 5+
Sandeep Mewara 15-Mar-11 6:01am    
Not at all. Infact, thanks. :)
Albin Abel 15-Mar-11 13:22pm    
Good informative links. My 5
Sergey Alexandrovich Kryukov 15-Mar-11 15:39pm    
Those references look useful, too, a 5.
--SA
Espen Harlinn 17-Mar-11 10:06am    
Good links, my 5
Please see my solution to this question here: how to open only one instance of class not more than that ?[^]. Eventhough it's for a windows form class the same principles apply for other classes too.
As Sandeep already pointed out there is a design pattern called Singleton, which will solve your problem.

To give you a hint:
The object creation from outside of the class is prohibited by making the constructor private. Anything marked private can only be accessed from within the class, but not from the outside.

Best Regards,
 
Share this answer
 
v2
Comments
Albin Abel 15-Mar-11 13:20pm    
My 5
Espen Harlinn 17-Mar-11 10:05am    
... a 5 here :)
Take a look at the Lazy<T> Class[^] - it provides an implementation of the singleton pattern.

Your implementation contains a small bug - you need to make getInstance() static too. The will enable you to get the single instance of OnlyOne using OnlyOne obj = OnlyOne::getInstance();
private static OnlyOne instance = null;

private OnlyOne()    
{
}    

public static OnlyOne getInstance()    
{        
 if(instance==null)
 {
  instance = new OnlyOne();
 }
 return instance;
}


From the documentation[^]:
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-Mar-11 6:26am    
You're correct! 5+
Fixed it.
Espen Harlinn 15-Mar-11 7:23am    
Thanks for the vote Manfred!
Manfred Rudolf Bihy 15-Mar-11 8:27am    
You're welcome!
Albin Abel 15-Mar-11 13:19pm    
Yes Lazy instantiation is a very good method with singleton and it has its thread safe. My 5+
Espen Harlinn 15-Mar-11 14:17pm    
Thanks for the vote AlbinAbel!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900