Export (0) Print
Expand All
Expand Minimize

Compiler Error CS1540

Cannot access protected member 'member' via a qualifier of type 'type1'; the qualifier must be of type 'type2' (or derived from it)

Although a derived class can access protected members of its base class, it cannot do so through an instance of the base class.

The following sample generates CS1540:

// CS1540.cs
public class Base
{
   protected void func()
   {
   }
}

public class Derived : Base
{
   public static void test(Base anotherInstance)
   // the method declaration could be changed as follows
   // public static void test(Derived anotherInstance)
   {
      anotherInstance.func();   // CS1540
   }
}

public class Tester : Derived
{
   public static void Main()
   {
      Base pBase = new Base();
      // the allocation could be changed as follows
      // Derived pBase = new Derived();
      test(pBase);
   }
}
Show:
© 2014 Microsoft