I have a class, that needs to call a refresh function every time a property is changed. So I find myself writing a lot of these:
private double _x;
public double X
{
get { return _x; }
set
{
_x = value;
refresh();
}
}
The refresh function is always the same for each property. Is there a shorter way to do this?
Also, I always access the private double _x through double X, so something like public double X { get; set} would work fine, if I could integrate the refresh() method somehow.
The pure C# way is to move the repetitive code to a method.
In your case, something like this:
void Set<T>(ref T field, T value)
{
field = value;
refresh();
}
and the use it like this:
private double _x;
public double X { get { return _x; } set { Set(ref _x, value); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With