I have a requirement where I have a number of classes all derived from a single base class. The base class contains lists of child classes also derived from the same base c
You can use the 'new' keyword but this will replace property, what you want is not possible with override.
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 new decimal Result
{
get { return _Result; }
set { _Result = value; }
}
}
I ended up changing the way I handled it and leaving the set in the base class however rather than having the base class getter / setter do nothing I threw a NotImplemented / NotSupported exception.
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result
{
get
{
if (Parent == null)
throw new NotImplementedException("Result property not valid");
return Parent.Result;
}
set
{
throw new NotSupportedException("Result property cannot be set here");
}
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public override decimal Result
{
get { return _Result; }
set { _Result = value; }
}
}