inheritance - C# How to add a property setter in derived class? -
i have requirement have number of classes derived single base class. base class contains lists of child classes derived same base class.
all classes need able obtain specific values may obtained class -or- it's parent depending on derived class is.
i looked @ using methods rather properties want make values available .net reporting component directly accesses exposed public properties in reporting engine excludes use of methods.
my question 'best practices' method of implementing setter in derivedclass without having publicly available setter in baseclass
public class baseclass { private baseclass _parent; public virtual decimal result { { return ((_parent != null) ? _parent.result : -1); } } } public class derivedclass : baseclass { private decimal _result; public override decimal result { { return _result; } // can't use setter here because there no set method in base class throws build error //set { _result = value; } } }
i can't add protected setter (as follows) in baseclass cannot change access modifiers in derivedclass.
public class baseclass { private baseclass _parent; public virtual decimal result { { return ((_parent != null) ? _parent.result : -1); } protected set { } } } public class derivedclass : baseclass { private decimal _result; public override decimal result { { return _result; } // setter throws build error because of change of access modifiers. //set { _result = value; } } }
i don't want add member variable in baseclass setter not want ability set property baseclass or other classes derive base.
public class baseclass { private baseclass _parent; protected decimal _result; // result in lot of unnecessary members in baseclass. public virtual decimal result { { return _result; } // empty setter ahead :) nothing. // throw exception here issues not found until runtime // , cause confusion in future new developers etc. set { } } } public class derivedclass : baseclass { public override decimal result { { return base.result; } set { base._result = value; } } }
other suggestions ?
you can use 'new' keyword replace property, want not possible override.
public class baseclass { private baseclass _parent; public virtual decimal result { { return ((_parent != null) ? _parent.result : -1); } } } public class derivedclass : baseclass { private decimal _result; public new decimal result { { return _result; } set { _result = value; } } }
Comments
Post a Comment