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.
The problem is that the
setin your derived class haspublicvisiblity—since you didn’t specifyprotectedexplicitly. Since this property’s set has protected visibility in your base class, you’re getting the errorYou can fix it by giving the set protected visibility in your derived class: