I want to toggle a button's visibility in when value of a particular variable changes. Is there a way to attach some kind of delegate to a variable which executes automatically when value changes?
A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program's execution. Thus, as its name implies – the value can vary.
While a variable's name, type, and location often remain fixed, the data stored in the location may be changed during program execution.
No, you can't do things like overloading assignment operator in C#. The best you could do is to change the variable to a property and call a method or delegate or raise an event in its setter.
private string field;
public string Field {
get { return field; }
set {
if (field != value) {
field = value;
Notify();
}
}
}
This is done by many frameworks (like WPF DependencyProperty
system) to track property changes.
Use Observer pattern. Here's another reference.
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