Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public class is inaccessible due to its protection level

Tags:

c#

I have the following classes:

namespace Bla.Bla  {     public abstract class ClassA      {         public virtual void Setup(string thing)          {         }          public abstract bool IsThingValid();          public abstract void ReadThings();          public virtual void MatchThings() { }          public virtual void SaveThings() { }          public void Run(string thing)          {             Setup(thing);              if (!IsThingValid())              {              }              ReadThings();             MatchThings();             SaveThings();         }     } }  namespace Bla.Bla  {     public class ClassB : ClassA      {         ClassB() { }           public override void IsThingValid()          {             throw new NotImplementedException();         }          public override void ReadThings()          {             throw new NotImplementedException();         }     } } 

Now I try to do the following:

public class ClassC  {     public void Main()      {         var thing = new ClassB();         ClassB.Run("thing");     } } 

Which returns the following error: ClassB is inaccessible due to its protection level.

But they are all public.

like image 674
jao Avatar asked Aug 23 '13 13:08

jao


People also ask

How do I fix inaccessible due to protection level in C#?

the Solution to the Error Because you did not assign any access modifier to your variables, they are set to be private as their default state. Take a look at the following code. You may have written your code like the above. You have to assign the access modifier to it.

Is not declared it may be inaccessible due to its protection level?

It may be inaccessible due to its protection level" error occurs when you declare a private method or property. As a result, you cannot access them. Please add the Protected or Public access level (see Access Levels in Visual Basic) to your "GetTitle" method's declaration and let me know your results.

How do I fix compiler error CS0122?

The error CS0122 is resolved by changing the member's access modifier to public .


1 Answers

This error is a result of the protection level of ClassB's constructor, not ClassB itself. Since the name of the constructor is the same as the name of the class* , the error may be interpreted incorrectly. Since you did not specify the protection level of your constructor, it is assumed to be internal by default. Declaring the constructor public will fix this problem:

public ClassB() { }  

* One could also say that constructors have no name, only a type; this does not change the essence of the problem.

like image 88
Sergey Kalinichenko Avatar answered Sep 20 '22 05:09

Sergey Kalinichenko