Solved

Restrict Second instatiation of a class

Posted on 2003-11-20
5
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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