Is there any shorthand in C# to make a setter additionally set a dirty flag
c#, setter
Solution
Create a Class where you implement a helper method.
class DirtyClass
{
protected bool IsDirty { get; set;}
protected void ChangeProperty<T>(ref T backing, T Value)
{
if(!backing.Equals(value))
{
backing = value;
IsDirty = true;
}
}
}
USe the helper method in the setter
class LivesCounter : DirtyClass
{
private int _lives;
public int Lives
{
get { return _lives; }
set { ChangeProperty(ref _lives, value); }
}
}
Handling null elements is left as an exercise.
As jdl134679 has mentioned, look into the INotifyPropertyChanged interface.
Problem
Currently in order to make a setter also set a dirty property I have to do something like this: ``` private bool _isDirty; private int _lives; public int Lives{ get { return _lives; } set { if (_lives != value){ _lives = value; _isDirty = true; } } } ``` It's not a huge pain to write but it's a very vertically spacious and repetitive piece of code to write if I use quite a lot of this pattern in my project. Is there any shorthand or alternative, shorter syntax to do that in C#? What I am specifically trying to accomplish is that certain variables changing should trigger a dirty flag, which on the render phase of the code can be used to refresh the properties of the rendered object.