C# Error CS1540 – Cannot access protected member ‘{0}’ via a qualifier of type ‘{1}’; the qualifier must be of type ‘{2}’ (or derived from it)

C# Error

CS1540 – Cannot access protected member ‘{0}’ via a qualifier of type ‘{1}’; the qualifier must be of type ‘{2}’ (or derived from it)

Reason for the Error & Solution

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

A derived cannot access protected members of its base class through an instance of the base class. An instance of the base class declared in the derived class might, at run time, be an instance of another type that is derived from the same base but is not otherwise related to the derived class. Because protected members can be accessed only by derived types, any attempts to access protected members that might not be valid at run time are marked by the compiler as not valid.

In the Employee class in the following example, emp2 and emp3 both have type Person at compile time, but emp2 has type Manager at run time. Because Employee is not derived from Manager, it cannot access the protected members of the base class, Person, through an instance of the Manager class. The compiler cannot determine what the run-time type of the two Person objects will be. Therefore, both the call from emp2 and the call from emp3 cause compiler error CS1540.

using System;  
using System.Text;  
  
namespace CS1540  
{  
    class Program1  
    {  
        static void Main()  
        {  
            Employee.PreparePayroll();  
        }  
    }  
  
    class Person  
    {  
        protected virtual void CalculatePay()
        {  
            Console.WriteLine("CalculatePay in Person class.");  
        }  
    }  
  
    class Manager : Person  
    {  
        protected override void CalculatePay()
        {  
            Console.WriteLine("CalculatePay in Manager class.");
  
        }  
    }  
  
    class Employee : Person  
    {  
        public static void PreparePayroll()  
        {  
            Employee emp1 = new Employee();  
            Person emp2 = new Manager();  
            Person emp3 = new Employee();  
            // The following line calls the method in the Employee base class,  
            // Person.  
            emp1.CalculatePay();
  
            // The following lines cause compiler error CS1540. The compiler
            // cannot determine at compile time what the run-time types of
            // emp2 and emp3 will be.  
            //emp2.CalculatePay();
            //emp3.CalculatePay();  
  
        }  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...