Reputation: 7672
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.
Upvotes: 6
Views: 11753
Reputation: 83366
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
}
}
}
Upvotes: 8