C # bool.change event

Is it possible to configure the event listener so that when the bool function is changed, the function is called?

+6
c # boolean
source share
4 answers

You must use the properties in C #, then you can add any processing you need to the setter (logging, triggering an event, ...)

private Boolean _boolValue public Boolean BoolValue { get { return _boolValue; } set { _boolValue = value; // trigger event (you could even compare the new value to // the old one and trigger it when the value really changed) } } 
+11
source share

Manually, yes you can

 public delegate void SomeBoolChangedEvent(); public event SomeBoolChangedEvent SomeBoolChanged; private bool someBool; public bool SomeBool { get { return someBool; } set { someBool = value; if (SomeBoolChanged != null) { SomeBoolChanged(); } } } 

Not sure, however, if that is what you are looking for.

+5
source share

The important question here is: when a bool what will change?

Since bool is a value type, you cannot directly pass references to it. Therefore, it makes no sense to talk about something like the Changed event on bool itself - if a bool changes, it is replaced by another bool , and not changed.

The image changes if we are talking about a field or a bool property for a reference type. In this case, the accepted practice is to show the bool as a property (public fields are not approved) and use the INotifyPropertyChanged.PropertyChanged event to raise the “modified” notification.

+5
source share

Take a look at the implementation of INotifyPropertyChanged. MSDN has an excellent on-topic How to

+4
source share

All Articles