Solved

Restrict Second instatiation of a class

Posted on 2003-11-20
5
234 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_
Comment Utility
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_
Comment Utility


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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
I need C# converted to VB.net 1 52
Build JSON from table records 17 39
Windows Service with UDP 2 26
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now