I have a requirement when I have several classes, all of which are produced from the same base class. The base class contains lists of child classes, also derived from the same base class.
All classes should be able to get specific values that can be obtained from the -OR-it parent class itself, depending on what the derived class is.
I looked at using methods, not properties, but I also want the values to be available for the .NET reporting component, which directly accesses public properties in the reporting engine, so this eliminates the use of methods.
My question is what will be the "best practices" method for implementing a setter in DerivedClass without having a public setter in BaseClass
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result
{
get { return ((_Parent != null) ? _Parent.Result : -1); }
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public override decimal Result
{
get { return _Result; }
}
}
I cannot add a protected setter (as follows) to BaseClass, since I cannot change access modifiers in DerivedClass.
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result {
get { return ((_Parent != null) ? _Parent.Result : -1); }
protected set { }
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public override decimal Result
{
get { return _Result; }
}
}
I do not want to add a member variable to BaseClass with the installer, because I do not need the ability to set the property from BaseClass or other classes that are produced from the base.
public class BaseClass
{
private BaseClass _Parent;
protected Decimal _Result;
public virtual decimal Result {
get { return _Result; }
set { }
}
}
public class DerivedClass : BaseClass
{
public override decimal Result
{
get { return base.Result; }
set { base._Result = value; }
}
}
Other offers?
source
share