Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
 Compiler Error CS1540
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual C# Reference: Errors and Warnings
Compiler Error CS1540

Error Message

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

A derived class cannot access protected members of its base class through another object instance of the base class if there are other classes in the inheritance hierarchy between it and the base class. This rule prevents a derived class from accessing base class methods that an intermediate class might have declared as sealed.

The following sample generates CS1540 and shows how to resolve the error by changing either the method signature or the new object expression to use Derived instead of Base.

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

public class Derived : Base
{
   public static void test(Base anotherInstance)
   // FIX #1: Change the method declaration as follows
   // public static void test(Derived anotherInstance)
   {
      anotherInstance.MethodA();   // CS1540
   }
}

public class Tester : Derived
{
   public static void Main()
   {
      Base myBaseClass = new Base();
      // FIX #2: Change the new object creation as follows
      // Derived myBaseClass = new Derived();
      test(myBaseClass);
   }
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker