Created
October 2, 2017 18:50
-
-
Save bugshake/d4a6ea578f2f96f07c5c6c2d701141e6 to your computer and use it in GitHub Desktop.
C# Observable Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get an event when a property changes | |
public class ObservableProperty<T> | |
{ | |
T value; | |
public delegate void ChangeEvent(T data); | |
public event ChangeEvent changed; | |
public ObservableProperty(T initialValue) | |
{ | |
value = initialValue; | |
} | |
public void Set(T v) | |
{ | |
if (!v.Equals(value)) | |
{ | |
value = v; | |
if (changed != null) | |
{ | |
changed(value); | |
} | |
} | |
} | |
public T Get() | |
{ | |
return value; | |
} | |
public static implicit operator T(ObservableProperty<T> p) | |
{ | |
return p.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Will output: