Single intance
-
hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...
P.gurukiran
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...
P.gurukiran
Hi, my you use an static variable within the class that counts the instances.
namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } }
You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards Erik -
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Christian Graus wrote:
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Ummmm.... I'm not sure how that describes a singleton. I've never used a Boolean flag in any of my singletons. They look a bit like this:
public SingletonClass
{
private static SingletonClass onlyInstance;// Private constructor - only this class can instantiate itself private SingletonClass() { } public SingletonClass Instance { get { if (onlyInstance == null) onlyInstance = new SingletonClass(); return onlyInstance; } } // Do your stuff here.
}
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
Hi, my you use an static variable within the class that counts the instances.
namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } }
You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards ErikI think the original poster was describing a singleton class.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
hi all, i want to know how to restrict the class not to instaciate more than once in C# help me in this plse...
P.gurukiran
What you are looking for is called a Singleton:
public SingletonClass
{
private static SingletonClass onlyInstance;// Private constructor - only this class can instantiate itself private SingletonClass() { } public SingletonClass Instance { get { if (onlyInstance == null) onlyInstance = new SingletonClass(); return onlyInstance; } } // Do your stuff here.
}
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
What you are looking for is called a Singleton:
public SingletonClass
{
private static SingletonClass onlyInstance;// Private constructor - only this class can instantiate itself private SingletonClass() { } public SingletonClass Instance { get { if (onlyInstance == null) onlyInstance = new SingletonClass(); return onlyInstance; } } // Do your stuff here.
}
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Christian Graus wrote:
A private constructor and a create method. A static boolean flag that starts as false, is set true by the Create method, and returns null once that flag is true.
Ummmm.... I'm not sure how that describes a singleton. I've never used a Boolean flag in any of my singletons. They look a bit like this:
public SingletonClass
{
private static SingletonClass onlyInstance;// Private constructor - only this class can instantiate itself private SingletonClass() { } public SingletonClass Instance { get { if (onlyInstance == null) onlyInstance = new SingletonClass(); return onlyInstance; } } // Do your stuff here.
}
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
where is the removed declared? and how is onlyInstance initialised?
-Prakash
-
Hi, my you use an static variable within the class that counts the instances.
namespace ConsoleApplication1 { class TestClass { // static variable public static int instanceCount = 0; // Constructor public TestClass() { instanceCount++; } // Some code // ... } class Program { static void Main(string[] args) { TestClass myTestClass1 = new TestClass(); TestClass myTestClass2 = new TestClass(); Console.WriteLine(TestClass.instanceCount); Console.ReadLine(); } } }
You can use the variable 'instanceCount' to restrict the instance generation. Is it that what you're looking for? Regards Erikyour code sample can not be used to retrict the number of instances, it only helps in couting the instances. As long as you keep the constructor public, it will be difficult (maybe impossible) to create a singleton class.
-Prakash
-
where is the removed declared? and how is onlyInstance initialised?
-Prakash
Mr.Prakash wrote:
where is the removed declared? and how is onlyInstance initialised?
The curious thing is that I thought I'd messed up my post, but when I went to modify it the code displays correctly in the edit window. "removed" should read "onlyInstance" and I have absolutely no idea why it says removed.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
Mr.Prakash wrote:
where is the removed declared? and how is onlyInstance initialised?
The curious thing is that I thought I'd messed up my post, but when I went to modify it the code displays correctly in the edit window. "removed" should read "onlyInstance" and I have absolutely no idea why it says removed.
Upcoming Scottish Developers events: * We are starting a series of events in Glasgow in 2007. Are you interested in a particular subject, or as a speaker? * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
Looks like Bob is teasing you a little!!:laugh:
Regards:rose: