Make certain classes only accessible once you've created an instance to the Client that holds those classes

475 Views Asked by At

I am making a class library. This class library has a class in it called Task Controller. It contains methods that rely on data that are formed in the primary client of the class library.

How can I make it so the Task controller class is only accessible once a coder has created an instance of the Client.

Want the coder to invoke it as following : Client.TaskController.foo()

1

There are 1 best solutions below

0
On

Maybe you want a public Property TaskController of the public class TaskController that itself can only be instantiated by your library using a private or internal constructor.

More information about internal keyword on MSDN.

public class TaskController
{
    // ...

    public void foo()
    {
         // ...
    }


    // internal constructor prevents object creation from outside your library
    internal TaskController()
    {
        // ...
    }
}

public class Client
{
    // ...

    // public constructor makes it possible to instantiate from outside the library
    public Client()
    {
        this.TaskController = new TaskController();
        // ...
    }

    // public property
    public TaskController TaskController { get; private set; }

}