Solved

Restrict Second instatiation of a class

Posted on 2003-11-20
5
254 Views
Last Modified: 2010-04-16
Hi all,

I have class, which should be instatiated only one in my project. If suppose u try to instatiate for the second time, it should return u some error message. Is it possoble. If yes how...thanks in advance
0
Comment
Question by:berylwilson
5 Comments
 
LVL 9

Expert Comment

by:_ys_
ID: 9795749
Sounds like you want to implement what's commonly referred to as a 'singleton'.

namespace MyNameSpace
{
     public class MyClass
     {
          /// <remarks>There can be only one</remarks>
          private static MyClass singleton = null;

          /// <remarks>Creates (if necessary) and returns the
          /// single instance</remarks>
          public static MyClass CreateInstance( )
          {
                    if (null == singleton)
                    {
                         singleton = new MyClass( );
                    }

                    return singleton;
          }

          /// <remarks>Private - Prevents public instantiation
          /// of MyClass</remarks>
          private MyClass( )
          {
          }
     }
}

Clients use MyClass.CreateInstance to 'create' their object.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9797663


Here is a web site that may help
http://msdn.microsoft.com/msdnmag/issues/01/07/patterns/


But I am pretty sure it has nearly the same information that ys provided
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 25 total points
ID: 9800855
The following variant is:
-easier to understand
-threadsafe (the above is not)
-also doesn't construct the singelton before any member of the class is accessed

namespace MyNameSpace
{
     public class MyClass
     {
          /// <remarks>Creates (if necessary)</remarks>
          private static MyClass singleton = new MyClass();

          /// <remarks>returns the single instance</remarks>
          public static MyClass Instance
          {
                    get
                    {
                         return singleton;
                    }
          }

....
0
 
LVL 5

Expert Comment

by:knowlton
ID: 10527697
Whatever happened with this?


I have a similar question

(Singleton class?)
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20909003.html


and

{Can changing over to a Singleton class help with this?)
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20908802.html


Urgent please help:


Tom


0
 
LVL 5

Expert Comment

by:knowlton
ID: 10527698
Whatever happened with this?


I have a similar question

(Singleton class?)
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20909003.html


and

{Can changing over to a Singleton class help with this?)
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20908802.html


Urgent please help:


Tom


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question