Cannot override property's protected set
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
}
}
}
Comments
-
atoMerz almost 2 years
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
andprivate
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 forVar
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. -
atoMerz over 12 yearsOh my god! Stupid VS! As I said in my post I had tried this and it did not help. I retried when you mentioned it, it still didn't work, I looked at your answer again, recompiled, and it's gone! So what do I do now? should I accept your answer or should I remove my post?
-
Adam Rackis over 12 years@AtoMerZ - I would accept the best answer and move on. Since the question has upvoted answers, you'll need a mod to remove it. Not worth the trouble for anyone.