Solved

Type intializer error on Singleton class

Posted on 2008-06-15
2
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 30

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groupbox Control ? 2 29
How to use spatial data types in SQL Server Database project 2 44
itextsharp with c# 3 18
C# LINQ 5 26
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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