Solved

Type intializer error on Singleton class

Posted on 2008-06-15
2
674 Views
Last Modified: 2013-12-17
Hi all

When I run the main windows application  and create the following singleiton class I get the following error.

public class HDPaint
    {
        #region Singleton implementation

        private HDPaint() { }

        /// <summary>
        /// Thread-safe singleton implementation
        /// </summary>
        public static readonly HDPaint currInstance = new HDPaint(); // error line

{"The type initializer for 'hdps.classLib.HdpsOrder' threw an exception."}

    }
0
Comment
Question by:AdibRahman77
2 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 21789613
Run it in the IDE with break-on-exception enabled, and you'll find out what the problem is, won't you?
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 21789775
Your implementation is wrong. You maty find classic implementation on msdn:
http://msdn.microsoft.com/en-us/library/ms998558.aspx.

Syntactically your code is OK. However, instance of a singleton should be returned through a public property. And the property has to return same instance for every call. In your case the public field returns new instance for every call - not a singleton at all.
public class Singleton

{

   private static Singleton instance;
 

   private Singleton() {}
 

   public static Singleton Instance

   {

      get 

      {

         if (instance == null)

         {

            instance = new Singleton();

         }

         return instance;

      }

   }

}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now