Izzy
Izzy

Reputation: 192

Object inaccessible due to it's protection level when referencing a member in an C# MVC controller

The Index() method of my controller references a type (ExternalSourceProvider) that's specified as a member at the start of the controller:

ExternalSourceProvider externalSource;

// GET: Index
public ActionResult Index()
{
    externalSource = new ExternalSourceProvider();

I'm getting an error for the ExternalSourceProvider(); on the last line, saying that it's inaccessible due to it's protection level. Here is the definition of ExternalSourceProvider:

public class ExternalSourceProvider
{
    ExternalSourceProvider() { }

    public string ExternalSiteAbsoluteURI { get; set; }

What am I missing?

Upvotes: 3

Views: 1587

Answers (2)

D Stanley
D Stanley

Reputation: 152634

Your default constructor is private (the default accessibility for a class member if none is specified) so there's no way to construct the object. Make it public:

public ExternalSourceProvider() { }

You could also make it internal if you only want other types within the same assembly to construct the type, but generally a public class should have at least one public constructor.

Upvotes: 4

Christos
Christos

Reputation: 53958

You have to mark the default constructor as public.

public class ExternalSourceProvider
{
    public ExternalSourceProvider() { }

    public string ExternalSiteAbsoluteURI { get; set; }
}

As it is now, it can't be accessed here

externalSource = new ExternalSourceProvider();

Upvotes: 6

Related Questions