C # How to add setter property to a derived class?

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 can't use a setter here because there is no set method in the base class so this throws a build error
      //set { _Result = value;  }
  }
}

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; }
    // This setter throws a build error because of a change of access modifiers.
    //set { _Result = value; }
  }
}

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; // This could result in a lot of unnecessary members in BaseClass.

  public virtual decimal Result {
    get { return _Result; } 
    // Empty setter ahead :) This does nothing.
    // I could also throw an exception here but then issues would not be found until runtime
    // and could cause confusion in the future with new developers etc.
    set { }
  }
}

public class DerivedClass : BaseClass
{
  public override decimal Result
  {
    get { return base.Result; }
    set { base._Result = value; }
  }
}

Other offers?

+4
source share
2 answers

You can use the 'new' keyword, but this will replace the property; what you want is not possible with an 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;  }
    }
}
+5
source

, , getter/setter , NotImplemented/NotSupported.

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;  }
  }
}
0

Source: https://habr.com/ru/post/1655465/


All Articles