Question

I have a question regarding inheritance in C#. I have a public abstract class A that defines protected virtual members. I have another abstract class B that inherits from A, and then another internal class C, inheriting from B. The first class A is in a separate namespace, and the other two are in the same namespace, but include a reference to the first.

I was trying to access the protected virtual member of A in C using base keyword, but am unable to unless I provide a method in B and then call A's protected virtual member there.

I want to know if what I was trying to do is possible, i.e. was I doing something incorrectly? Or if its not possible, then why? Thanks.

Here's the code sample :

  namespace A
  {

     public abstract class BaseClass
     {
         protected virtual string GetData()
         {
             //code to get data
         }
     }
  }

  namespace B  //includes a reference to A
  {
     abstract class DerivedClassA : BaseClass
     {

     }


     internal class DerivedClassB: DerivedClassA
     {
          public void write()
          {
             base.GetData(); // results in error.

             // The name 'GetData' does not exist in the current context
             // and DerivedClassA does not contain a definition for 'GetData'
          }
     }
Was it helpful?

Solution 3

Got the source of the problem. Apparently, the location of the dlls that the second namespace was referring to had changed and hence, it was pointing to an outdated reference. After I updated the references from the correct location, it ran fine. Thanks all for the help.

OTHER TIPS

Other than a namespace issue your code ran fine, the one thing I had to change was change DerivedClassA to

abstract class DerivedClassA : A.BaseClass
{

}

What I suspect is whatever BaseClass is called in your real project you have a class of the same name in namespace B or some other namespace you have included in your using statements at the top of your code. For example this would recreate your error

  namespace A
  {

     public abstract class BaseClass
     {
         protected virtual string GetData()
         {
             throw new NotImplementedException();
         }
     }
  }

  namespace B  //includes a reference to A
  {
     public abstract class BaseClass
     {
     }


     abstract class DerivedClassA : BaseClass
     {

     }


     internal class DerivedClassB: DerivedClassA
     {
          public void write()
          {
             base.GetData(); // results in error.

             // The name 'GetData' does not exist in the current context
             // and DerivedClassA does not contain a definition for 'GetData'
          }
     }
  }

Check that you don't have another namespace that uses the same name as BaseClass or explicitly define the namespace of BaseClass in DerivedClassA

This works for me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Protectedmemberinabstractclassex
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
namespace A
{

    public abstract class BaseClass
    {
        protected virtual string GetData()
        {
            throw new NotImplementedException();
        }
    }
}

namespace B  //includes a reference to A
{


    abstract class DerivedClassA : A.BaseClass
    {

    }


    internal class DerivedClassB : DerivedClassA
    {
        public void write()
        {
            base.GetData();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top