Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Restrict Second instatiation of a class

Posted on 2003-11-20
5
Medium Priority
?
278 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 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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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