Singleton example

Hi you experts.

Can you give an example of a singleton class?
I need an example in which a property of a class is filled(data are passed to the class).


Thank you all in advance
LVL 1
adktdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AGBrownCommented:
Depending on your use for the singleton your declaration structure will change, but for a thoroughly thread-safe singleton, you can do:

      public class SingletonUser
      {
            public void SetSingletonPropertyA(string theValue)
            {
                  MySingletonClass.Instance.PropertyA = theValue;
            }
      }
      public sealed class MySingletonClass
      {
            #region Singleton implementation
            /// <summary>
            ///            Thread-safe singleton implementation, bypassing the need for lazy implementation and double-lock check.
            /// </summary>
            public static readonly MySingletonClass Instance = new MySingletonClass();
            /// <summary>
            ///            Private so that only this class can declare itself
            /// </summary>
            private MySingletonClass() {}
            #endregion Singleton implementation
            private string _PropertyA;
            public string PropertyA
            {
                  get
                  {
                        return this._PropertyA;
                  }
                  set
                  {
                        this._PropertyA = value;
                  }
            }
      }

There is a very good MSDN article on singletons here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp). It is a bit old, but I don't know of any more recent articles.

Andy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adktdAuthor Commented:
Thank you so much.I'll try it and i'll let you know how it goes.
0
AGBrownCommented:
No problem

Andy
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

bsmuehmerCommented:
I only want to add another MSDN article:
       http://msdn.microsoft.com/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp

It is like the other one, but IMHO better shows the different aspects of how to implement it with C#/.Net.
0
AGBrownCommented:
The interesting thing about that article is that it mentions only three options for creating your singleton:
1) Non-thread safe, lazy initialisation
2) Non-thread safe using readonly.
3) Double-check locking lazy initialisation and volatile mark.

The other article adds the fourth method that I posted above which is (was at the time of writing? maybe is now possible?) not possible in Java; a .NET specific implementation almost exactly the same as the 3rd, but a lot simpler. This is still lazily initialised; is thread-safe; and still gets around the problem wherein some compilers reorder the lazy initialisation, and reintroduce the thread-safety issues that the double-lock check tries to get rid of. All of it is done by relying on the .NET framework behaviours.

Its also a lot, lot simpler to write the third method than either of the first two.

I don't yet know of any reason not to use this implementation in favour of any of the first 3. I would love to be proved wrong though!

Andy
0
AGBrownCommented:
adktd,

Did you have any luck with this?

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.