Singleton example

adktd
adktd used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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

Author

Commented:
Thank you so much.I'll try it and i'll let you know how it goes.

Commented:
No problem

Andy
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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.

Commented:
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

Commented:
adktd,

Did you have any luck with this?

Andy

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial