Error: An object reference is required for the non-static field, method or property

| By Webner

Object Reference

In one of my .net reference projects, I had a need to create a new model and its corresponding interface, repository, and client class. In a method of the existing controller class, I need to create a record of that model class but I don’t want to use this new class’s interface as dependency injection for. So to call the method from the repository class, I tried to directly access the method using the name of the Interface or the repository class like:


Here, ITestRepository is the interface and TestRepositoryClient class is implementing the interface ITestRepository.

But I was getting the following error in this way:
An object reference is required for the non-static field, method, or property

Then I tried to create an object from the interface class like below:

ITestRepository myTestRepo = new ITestRepository();

But it also gave me the following error:
Cannot create an instance of the abstract class or interface
So to resolve this issue, I created an instance of the repository like below:

ITestRepository myTestRepo = new TestRepositoryClient();

And called method on this object like below:


So this is the way to create an instance of a repository class using an interface.

Leave a Reply

Your email address will not be published. Required fields are marked *