473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Simply a Singleton - in C# .NET

I have implemented singleton classes many times in the past without a problem. Perhaps dimentia is setting in, but today I found I couldn't make it work.
I'm using Visual Studio 2008, C#, and started with a simple:
Expand|Select|Wrap|Line Numbers
  1.     public class justOne
  2.     {
  3.         private static justOne _justOne = null;
  4.  
  5.         public static int LocalCounter;
  6.  
  7.         private justOne() { LocalCounter = 0; }
  8.         public justOne getJustOne()
  9.         {
  10.             if (_justOne == null)
  11.                 _justOne = new justOne();
  12.             return _justOne;
  13.         }
  14.     }
  15.  
I run a small app in the same solution and watch it start. From a similar program (different solution), I reference the same class library, but it creates a new instance even though the first one is still running.

I read http://msdn.microsoft.com/en-us/library/ff650316.aspx and tried every variation suggested, but no luck.

Okay, where did I go wrong?

Thanks in Advance! :-)
Jul 24 '10 #1

✓ answered by Alex Papadimoulis

You are correct; it is logically impossible to have code executing in different processes share the same memory space (and therefore, an instance of an object). When a new process starts up, it gets its own stack/heap space.

Inter-process state-management is the only way to do this, and a server process (such as a WCF endpoint) can function in this capacity.

Alternatively, you could maintain the state in a database. This might be easier and prefered as it will be more reliable (consider: if the application crashes/restarts) at maintaining your state data.

4 1614
I did notice: I had forgotten to add static in the singleton class function. It should read:
public static justOne getJustOne()

:-)
Jul 24 '10 #2
I found that if I call getJustOne multiple times in the same application, I do - in fact - get just the same instance. What I want/need is for this to work regardless of how many different applications access the same class library (dll). This is what isn't working.

Thanks.
Jul 24 '10 #3
To answer my own question: a simple singleton as I described above will be instantiated as a new class instance across different processes UNLESS I incorporate some method of remoting such as WCF.... Thus, I will be adding WCF to the mix.
Jul 25 '10 #4
Alex Papadimoulis
26 Expert
You are correct; it is logically impossible to have code executing in different processes share the same memory space (and therefore, an instance of an object). When a new process starts up, it gets its own stack/heap space.

Inter-process state-management is the only way to do this, and a server process (such as a WCF endpoint) can function in this capacity.

Alternatively, you could maintain the state in a database. This might be easier and prefered as it will be more reliable (consider: if the application crashes/restarts) at maintaining your state data.
Aug 3 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alexiev Nikolay | last post by:
I need a simple implementation of singleton that will work on python 2.1. I havn't got idea how to creat without staticmethod. Can you give me a solution ??? 10x in advance ...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
7
by: PMarino | last post by:
Hi all - I've taken some code from MSDN and made a Generic singleton, so that I don't have to write this code in many places: public class Singleton<TEntity> where TEntity : class, new() {...
7
by: ThunderMusic | last post by:
Hi, I have a problem regarding singletons in C#. What I would like to do is the following ClassA is a singleton ClassB inherits from ClassA and is also a Singleton there cannot and instance of...
9
by: oleggus | last post by:
I hope, I misunderstood some basics here and it is easy to solve.. I need a singleton object running on server which can be used(write and read) by different client interfaces - for example there...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.