Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot override property's protected set

I have the following base class:

abstract class Base
{
 public abstract object Var
 {
  get;
  protected set;
 }
}

And this derived class:

class Derived : Base
{
 public override object Var
 {
  get {//code here
  }
  set {//code here -- I get error here!
  }
 }
}

But I'm getting this error:

Cannot change access modifier when overriding 'protected' inherited member 'Var'

I tried adding a protected and private keywords before set but it didn't help. How do I fix this?

UPDATE:
The base class must make sure that subclasses provide a value for Var at creation time. So I need to have the setter declared in Base class.
Alternatively, I could declare a private member variable to do this and remove the setter, but that is not an option as discussed here.

like image 464
atoMerz Avatar asked Dec 31 '11 18:12

atoMerz


1 Answers

The problem is that the set in your derived class has public visiblity—since you didn't specify protected explicitly. Since this property's set has protected visibility in your base class, you're getting the error

cannot change access modifiers when overriding 'protected' inherited member

You can fix it by giving the set protected visibility in your derived class:

class Derived : Base {
    public override object Var {
        get { return null; }
        protected set { // <------ added protected here
        }
    }
}
like image 170
Adam Rackis Avatar answered Sep 28 '22 18:09

Adam Rackis