Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton in C# "inaccessible" when not in the same namespace?

I tried to implement a singleton class in the following way (I use VS2008 SP1) :

namespace firstNamespace
{
   class SingletonClass
   {
      private SingletonClass() {}

      public static readonly SingletonClass Instance = new SingletonClass();
   }
}

When I want to access it from a class in a different namespace (it seems that this is the problem, in the same namespace it works) like :

namespace secondNamespace
{
   ...
   firstNamespace.SingletonClass inst = firstNamespace.SingletonClass.Instance;
   ...
}

I get a compiler error:

error CS0122: 'firstNamespace.SingletonClass' is inaccessible due to its protection level

Does somebody have an idea how to solve this?

Many thanks in advance!

like image 436
Jakob S. Avatar asked Jan 26 '11 09:01

Jakob S.


People also ask

What is a singleton in C?

Singleton in C++ Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables.

What is singleton give example?

Example of singleton classes is Runtime class, Action Servlet, Service Locator. Private constructors and factory methods are also an example of the singleton class.

What is singleton used for?

Singleton pattern is used for logging, drivers objects, caching, and thread pool. Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade, etc. Singleton design pattern is used in core Java classes also (for example, java. lang.

What are the singletons?

singleton (plural singletons) (playing cards) A playing card that is the only one of its suit in a hand, especially at bridge. (playing cards) A hand containing only one card of a certain suit.


2 Answers

You're missing the keyword public from your class definition.

like image 195
Moo-Juice Avatar answered Sep 27 '22 23:09

Moo-Juice


Sounds more like the singleton is in a different assembly. The default modifier for a class is internal and thereby only accessible in the assembly.

like image 37
Emond Avatar answered Sep 27 '22 23:09

Emond