singleton class please help 
Author Message
 singleton class please help

Hi
I am trying to create a singleton class. I have a static member variable
that stores the same class instance. I want to create it only once. But
somehow it is not happeneing and it is creating a new instance for each
application that i try to access. Pl find below my code.

if anyone can throw any idea on this, it will be of great help.

thanks

naveen

sealed public class CSingletonClass

{

public static CSingletonClass Instance= null;

private string m_ConnectionString =null;

public CSingletonClass()

{

Debug.WriteLine("Constructor called");

Quote:
}

public static CSingletonClass GetInstance()

{

if(Instance==null)

{

Debug.WriteLine("Create a new instance");

Instance = new CSingletonClass();

return Instance;

Quote:
}

else

{

return Instance;

Quote:
}
}

public string GetConnectionString()

{

if(m_ConnectionString==null)

{

//get the connection string form the database..

Debug.WriteLine("getting the connection string from the database");

m_ConnectionString = "New actual connection string";

return m_ConnectionString;

Quote:
}

else

{

return m_ConnectionString;

Quote:
}
}

public string ConnectionString

{

get

{

Debug.WriteLine("Returning the connection string");

return m_ConnectionString;

Quote:
}

set

{

Debug.WriteLine("Getting the connection string");

m_ConnectionString = value;

Quote:
}
}



Tue, 14 Jun 2005 01:01:04 GMT  
 singleton class please help
Hi, Naveen.
  As a pattern, Singleton only works within address space boundary of an
application. For win32, it's Process. While for .NET application, it's
AppDomain.
  To make it work system wide, you have to add some other mechanism. For
example, you could use .Net Remoting: Start an object server which provides
the singleton instance, and all other applications access the object through
remoting.

  Hope this helps.
  Ming Chen [MVP]


Quote:
> Hi
> I am trying to create a singleton class. I have a static member variable
> that stores the same class instance. I want to create it only once. But
> somehow it is not happeneing and it is creating a new instance for each
> application that i try to access. Pl find below my code.

> if anyone can throw any idea on this, it will be of great help.

> thanks

> naveen

> sealed public class CSingletonClass

> {

> public static CSingletonClass Instance= null;

> private string m_ConnectionString =null;

> public CSingletonClass()

> {

> Debug.WriteLine("Constructor called");

> }

> public static CSingletonClass GetInstance()

> {

> if(Instance==null)

> {

> Debug.WriteLine("Create a new instance");

> Instance = new CSingletonClass();

> return Instance;

> }

> else

> {

> return Instance;

> }

> }

> public string GetConnectionString()

> {

> if(m_ConnectionString==null)

> {

> //get the connection string form the database..

> Debug.WriteLine("getting the connection string from the database");

> m_ConnectionString = "New actual connection string";

> return m_ConnectionString;

> }

> else

> {

> return m_ConnectionString;

> }

> }

> public string ConnectionString

> {

> get

> {

> Debug.WriteLine("Returning the connection string");

> return m_ConnectionString;

> }

> set

> {

> Debug.WriteLine("Getting the connection string");

> m_ConnectionString = value;

> }

> }



Tue, 14 Jun 2005 01:31:47 GMT  
 singleton class please help
Are you sure you use the following to get the instance:

CSingletonClass cs = CSingletonClass.GetInstance();

CSingletonClass cs1 = CSingletonClass.GetInstance();

CSingletonClass cs2 = CSingletonClass.GetInstance();

Doing this, you will get one and only one instance.

Jos


Quote:
> Hi
> I am trying to create a singleton class. I have a static member variable
> that stores the same class instance. I want to create it only once. But
> somehow it is not happeneing and it is creating a new instance for each
> application that i try to access. Pl find below my code.

> if anyone can throw any idea on this, it will be of great help.

> thanks

> naveen

> sealed public class CSingletonClass

> {

> public static CSingletonClass Instance= null;

> private string m_ConnectionString =null;

> public CSingletonClass()

> {

> Debug.WriteLine("Constructor called");

> }

> public static CSingletonClass GetInstance()

> {

> if(Instance==null)

> {

> Debug.WriteLine("Create a new instance");

> Instance = new CSingletonClass();

> return Instance;

> }

> else

> {

> return Instance;

> }

> }

> public string GetConnectionString()

> {

> if(m_ConnectionString==null)

> {

> //get the connection string form the database..

> Debug.WriteLine("getting the connection string from the database");

> m_ConnectionString = "New actual connection string";

> return m_ConnectionString;

> }

> else

> {

> return m_ConnectionString;

> }

> }

> public string ConnectionString

> {

> get

> {

> Debug.WriteLine("Returning the connection string");

> return m_ConnectionString;

> }

> set

> {

> Debug.WriteLine("Getting the connection string");

> m_ConnectionString = value;

> }

> }



Tue, 14 Jun 2005 01:27:08 GMT  
 singleton class please help
hi
you are right. im trying to access it from various other applications and
that's why im getting different instances

thanks

naveen

Quote:
> Are you sure you use the following to get the instance:

> CSingletonClass cs = CSingletonClass.GetInstance();

> CSingletonClass cs1 = CSingletonClass.GetInstance();

> CSingletonClass cs2 = CSingletonClass.GetInstance();

> Doing this, you will get one and only one instance.

> Jos



> > Hi
> > I am trying to create a singleton class. I have a static member variable
> > that stores the same class instance. I want to create it only once. But
> > somehow it is not happeneing and it is creating a new instance for each
> > application that i try to access. Pl find below my code.

> > if anyone can throw any idea on this, it will be of great help.

> > thanks

> > naveen

> > sealed public class CSingletonClass

> > {

> > public static CSingletonClass Instance= null;

> > private string m_ConnectionString =null;

> > public CSingletonClass()

> > {

> > Debug.WriteLine("Constructor called");

> > }

> > public static CSingletonClass GetInstance()

> > {

> > if(Instance==null)

> > {

> > Debug.WriteLine("Create a new instance");

> > Instance = new CSingletonClass();

> > return Instance;

> > }

> > else

> > {

> > return Instance;

> > }

> > }

> > public string GetConnectionString()

> > {

> > if(m_ConnectionString==null)

> > {

> > //get the connection string form the database..

> > Debug.WriteLine("getting the connection string from the database");

> > m_ConnectionString = "New actual connection string";

> > return m_ConnectionString;

> > }

> > else

> > {

> > return m_ConnectionString;

> > }

> > }

> > public string ConnectionString

> > {

> > get

> > {

> > Debug.WriteLine("Returning the connection string");

> > return m_ConnectionString;

> > }

> > set

> > {

> > Debug.WriteLine("Getting the connection string");

> > m_ConnectionString = value;

> > }

> > }



Tue, 14 Jun 2005 03:07:07 GMT  
 singleton class please help
hi
thank you very much. Yes i think im trying to access them from two different
process. That is why im getting two instances. Could you explain me further
on how to make it available for process wide with the same instance. Do i
just have to access the class thro remoting? or do i need to do anything
extra. or is there any example available?

thanks

naveen

Quote:
> Hi, Naveen.
>   As a pattern, Singleton only works within address space boundary of an
> application. For win32, it's Process. While for .NET application, it's
> AppDomain.
>   To make it work system wide, you have to add some other mechanism. For
> example, you could use .Net Remoting: Start an object server which
provides
> the singleton instance, and all other applications access the object
through
> remoting.

>   Hope this helps.
>   Ming Chen [MVP]



> > Hi
> > I am trying to create a singleton class. I have a static member variable
> > that stores the same class instance. I want to create it only once. But
> > somehow it is not happeneing and it is creating a new instance for each
> > application that i try to access. Pl find below my code.

> > if anyone can throw any idea on this, it will be of great help.

> > thanks

> > naveen

> > sealed public class CSingletonClass

> > {

> > public static CSingletonClass Instance= null;

> > private string m_ConnectionString =null;

> > public CSingletonClass()

> > {

> > Debug.WriteLine("Constructor called");

> > }

> > public static CSingletonClass GetInstance()

> > {

> > if(Instance==null)

> > {

> > Debug.WriteLine("Create a new instance");

> > Instance = new CSingletonClass();

> > return Instance;

> > }

> > else

> > {

> > return Instance;

> > }

> > }

> > public string GetConnectionString()

> > {

> > if(m_ConnectionString==null)

> > {

> > //get the connection string form the database..

> > Debug.WriteLine("getting the connection string from the database");

> > m_ConnectionString = "New actual connection string";

> > return m_ConnectionString;

> > }

> > else

> > {

> > return m_ConnectionString;

> > }

> > }

> > public string ConnectionString

> > {

> > get

> > {

> > Debug.WriteLine("Returning the connection string");

> > return m_ConnectionString;

> > }

> > set

> > {

> > Debug.WriteLine("Getting the connection string");

> > m_ConnectionString = value;

> > }

> > }



Tue, 14 Jun 2005 03:06:34 GMT  
 singleton class please help
Well, if what you want is only keep one object instance system wide, it's
not hard.
You could have a system wide symbol to identify whether the "one-and-only"
object has been created.
For example, you could use Mutex with following ctor and set name to
something special (say, a GUID):
    public Mutex(
       bool initiallyOwned,
       string name,
       out bool createdNew
    );

The Application which gets createdNew equals true could go ahead and create
the object.
For others, the object already exists.

But after that, the real problem comes: how can you use the singleton object
across process/appdomain boundary?
As I can see, remoting is the best solution. For the application which
creates the object, you could also make it running as a remoting server. So
that other applications could retrive the object through a remoting channel.

Hope this helps.
Ming Chen [MVP]


Quote:
> hi
> thank you very much. Yes i think im trying to access them from two
different
> process. That is why im getting two instances. Could you explain me
further
> on how to make it available for process wide with the same instance. Do i
> just have to access the class thro remoting? or do i need to do anything
> extra. or is there any example available?

> thanks

> naveen


> > Hi, Naveen.
> >   As a pattern, Singleton only works within address space boundary of an
> > application. For win32, it's Process. While for .NET application, it's
> > AppDomain.
> >   To make it work system wide, you have to add some other mechanism. For
> > example, you could use .Net Remoting: Start an object server which
> provides
> > the singleton instance, and all other applications access the object
> through
> > remoting.

> >   Hope this helps.
> >   Ming Chen [MVP]



> > > Hi
> > > I am trying to create a singleton class. I have a static member
variable
> > > that stores the same class instance. I want to create it only once.
But
> > > somehow it is not happeneing and it is creating a new instance for
each
> > > application that i try to access. Pl find below my code.

> > > if anyone can throw any idea on this, it will be of great help.

> > > thanks

> > > naveen

> > > sealed public class CSingletonClass

> > > {

> > > public static CSingletonClass Instance= null;

> > > private string m_ConnectionString =null;

> > > public CSingletonClass()

> > > {

> > > Debug.WriteLine("Constructor called");

> > > }

> > > public static CSingletonClass GetInstance()

> > > {

> > > if(Instance==null)

> > > {

> > > Debug.WriteLine("Create a new instance");

> > > Instance = new CSingletonClass();

> > > return Instance;

> > > }

> > > else

> > > {

> > > return Instance;

> > > }

> > > }

> > > public string GetConnectionString()

> > > {

> > > if(m_ConnectionString==null)

> > > {

> > > //get the connection string form the database..

> > > Debug.WriteLine("getting the connection string from the database");

> > > m_ConnectionString = "New actual connection string";

> > > return m_ConnectionString;

> > > }

> > > else

> > > {

> > > return m_ConnectionString;

> > > }

> > > }

> > > public string ConnectionString

> > > {

> > > get

> > > {

> > > Debug.WriteLine("Returning the connection string");

> > > return m_ConnectionString;

> > > }

> > > set

> > > {

> > > Debug.WriteLine("Getting the connection string");

> > > m_ConnectionString = value;

> > > }

> > > }



Tue, 14 Jun 2005 04:30:45 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. ATL Singleton and Singleton Class also from C++

2. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

3. I am not able to see my database class members in class view - Please help

4. Please help!!!!Please help!!!!Please help!!!!

5. Singleton not a singleton?

6. Singleton class factory using ROT???

7. Singleton Adapter Class

8. Utilizing a Singleton Class Factory

9. ATL Singleton Class

10. How to prevent class(derived from Singleton) from being declared twice

11. Inheriting from a Singleton class

12. How to make class singleton?

 

 
Powered by phpBB® Forum Software