Escage NULL
Escage NULL

Reputation: 35

C# Trouble initializing class

I'm trying to initialize a class like this:

DllInjector mInject = new DllInjector();

but DllInjector() gives me error: "is inaccessable due to its protection level" So I looked through the class and saw it had a singleton:

public static DllInjector GetInstance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new DllInjector();
            }
            return _instance;
        }
    }

So how do I instantiate the class and use it?

Upvotes: 1

Views: 91

Answers (2)

Nirav Kamani
Nirav Kamani

Reputation: 3272

Singleton is design pattern.

What does it mean :-

It means you can only create one instance of a class and the constructor of that class is made private so it can not be instantiated from outside and actually the instance of that singleton is created within that class only and return using some properties or methods.

For more information you can check :-

Implementing Singleton in C#

And for deep dive you can go with one and only Jon Skeet

Implementing the Singleton Pattern in C#

Upvotes: 0

enkryptor
enkryptor

Reputation: 1683

The Singleton pattern implies you have only one single instance of the class.

So you can't just instantiate another one using the constructor:

var mInject = new DllInjector();

The constructor isn't public, that's why you get the "is inaccessible due to its protection level" error. Instead, you are supposed to use the provided accessor to the existing instance:

var mInject = DllInjector.GetInstance;

If you call it for the first time, it will be instantiated automatically.

Upvotes: 1

Related Questions