Click to See Complete Forum and Search --> : forcing only one instance
dky1e
June 27th, 2002, 11:36 AM
Hi,
How can I force only one instance at a time of a class in c#.
Is defining the class as static in the first class that is loaded good enough?
that is:
class1
{
public static c val1 = new c();
[...]
}
class2
{
private c val2 = new c() //will have the same instance as val1?
}
Thanks.
StephaneDaigle
June 27th, 2002, 11:59 AM
You need to create a singleton. You need to have something like this:
public static SomeClass getUniqueInstance()
{
if(m_UniqueInstance == null)
{
m_UniqueInstance = new SomeClass;
}
return m_UniqueInstance;
}
Now, whenever you need the instance to the class SomeClass, call the static getUniqueInstance() method and it will either create the first instance or return the existing instance.
jparsons
June 27th, 2002, 12:13 PM
Originally posted by StephaneDaigle
You need to create a singleton. You need to have something like this:
public static SomeClass getUniqueInstance()
{
if(m_UniqueInstance == null)
{
m_UniqueInstance = new SomeClass;
}
return m_UniqueInstance;
}
Now, whenever you need the instance to the class SomeClass, call the static getUniqueInstance() method and it will either create the first instance or return the existing instance.
Note that this is not a thread safe way to ensure that there will only be one class. You have to add some locking
public static Object m_lock = new Object();
pubilc static SomeClass getUniqueInstance() {
lock ( m_lock ) {
if ( m_UniqueInstance == null ) {
m_UniqueInstance = new SomeClass();
}
return m_UniqueInstance;
}
}
dky1e
June 27th, 2002, 02:22 PM
How does lock work?
why is m_lock of type Object?
How do I use it? Do I need to create a separate class and call it the singleton or do I just put the method inside the class that I want to make unique?
How do I create an instance of the class in other classes?
Thanks for your help.
StephaneDaigle
June 27th, 2002, 02:46 PM
You just need to add the static method inside your class and call it to retrieve the instance of that class. This call can be done from any other class you are using if you want all the classes to have the same instance of your unique class.
To make the code thread safe, you could also use the lock keyword.
lock(this)
{
....
}
:)
jparsons
June 27th, 2002, 02:49 PM
Originally posted by StephaneDaigle
You just need to add the static method inside your class and call it to retrieve the instance of that class. This call can be done from any other class you are using if you want all the classes to have the same instance of your unique class.
To make the code thread safe, you could also use the lock keyword.
lock(this)
{
....
}
:)
There is no "this" inside of a static method
StephaneDaigle
June 27th, 2002, 02:55 PM
That's true, I forgot... thanks!
Arild Fines
June 28th, 2002, 12:49 PM
Wouldn't
lock( typeof( SomeClass ) )
{
//...
}
be better?
jparsons
July 1st, 2002, 08:52 AM
Originally posted by Arild Fines
Wouldn't
lock( typeof( SomeClass ) )
{
//...
}
be better?
No. typeof(SomeClass) could be creating objects everytime you call it so you would never be locking the same object. This would cause the locking to be pointless.
Arild Fines
July 1st, 2002, 05:36 PM
I believe thou art mistaken - from the docs for the Type class:
A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for the synchronization of multiple static method invocations and for comparison of Type objects using reference equality.
jparsons
July 1st, 2002, 09:04 PM
Originally posted by Arild Fines
I believe thou art mistaken - from the docs for the Type class:
Sounds correct to me.
Arild Fines
July 2nd, 2002, 04:15 PM
Hmm... that was a bit ambiguous - did you agree with my interpretation or no?
Anyway - as I see that paragraph, it means that there will never be more than one Type object for each existing type in the system.
Object o = new Object();
Type t1 = o.GetType();
Type t2 = typeof( Object );
In this case, t1 and t2 will in fact refer to the same Type object.
jparsons
July 3rd, 2002, 12:47 AM
Originally posted by Arild Fines
Hmm... that was a bit ambiguous - did you agree with my interpretation or no?
Anyway - as I see that paragraph, it means that there will never be more than one Type object for each existing type in the system.
Object o = new Object();
Type t1 = o.GetType();
Type t2 = typeof( Object );
In this case, t1 and t2 will in fact refer to the same Type object.
I agree with your interpretation. I think that it's a bit odd of C# to do that but I'm sure that there are plenty of reasons for it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.