I have a following statement:
public int aValue { get; set; }
public int func() { return someNonConstantValue; }
is there a way to constraint it to the range from -5 to what func() returns?
I could use Math.Clamp but if I define the setter will I also need to provide the getter?
>Solution :
Yes, if you provide a setter you will also need a custom getter. By this should not be a problem:
private int _aValue;
public int aValue
{
get => _aValue;
set
{
int upperLimit = func();
if(value < -5 || value > upperLimit )
{
throw new ArgumentOutOfRangeException("value", "should be between -5 and " + upperLimit);
}
_aValue = value;
}
}