?
Solved

Type intializer error on Singleton class

Posted on 2008-06-15
2
Medium Priority
?
683 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 1500 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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