Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Restrict Second instatiation of a class

Posted on 2003-11-20
5
Medium Priority
?
283 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 75 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:Tom 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:Tom 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

886 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