Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:44:23+00:00 2026-05-26T04:44:23+00:00

This is a relatively straight forward question. But I was wondering what the correct

  • 0

This is a relatively straight forward question. But I was wondering what the correct usage is for accessing a method inside a separate project through the use of an interface.

Project: Test.ClassLibrary

Interface:

public interface ITest
{
    string TestMethod();
}

Class:

public class Test : ITest
{
    public string TestMethod()
    {
        return "Test";
    }
}

Project: Test.Web

Controller:

public class HomeController : Controller
{
    private ITest test;
    public ActionResult Index()
    {
        return Content(test.TestMethod());
    }

}

The above returns a NullReferenceException. I’m assuming it’s because the controller gets to the interface, and doesn’t know where to go next.

What’s the best way to fix this? Do I have to reference the Test class in the controller or can I some how get away with only having a reference to ITest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:44:24+00:00Added an answer on May 26, 2026 at 4:44 am
    • You never instantiate ITest test, you only declare it.
    • Your Test class doesn’t inherit from the interface.

    You need to update your class declaration

    public class Test : ITest // interface inheritance 
    {
    

    And in your controller, instantiate test.

    ITest test = new Test();
    

    As you get further along, you’ll want to explore techniques for injecting the Test instance into the controller so that you do not have a hard dependency upon it, but just on the interface ITest. A comment mentions IoC, or Inversion of Control, but you should look into various Dependency Inversion techniques techniques (IoC is one of them, dependency injection, etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a relatively straight forward question but one that has me stumped. In
I'm relatively new to the Python world, but this seems very straight forward. Google
This may be a relatively straight-forward question that I just haven't been searching for
I hope this is a relatively straight forward thing to do, and that my
So the question is relatively straight forward, I have several semi-large lookup tables ~500kb
I am sure there must be a relatively straightforward way to do this, but
I'm using WCF through Spring.net WCF integration link text This works relatively fine, however
I realised this might be relatively niche, but maybe that's why this is good
I'm sure this is a relatively simple question, and there must be a good
I'm relatively new to web application programming so I hope this question isn't too

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.