How to trigger an event when a variable value changes?

I am currently building a C # application using Visual Studio. I want to create some code, so when the variable has a value of 1, a certain part of the code is executed. I know that I can use the if statement, but the problem is that the value will be changed in the asynchronous process, so technically the if statement can be ignored before the value is changed.

Is it possible to create an event handler so that when a variable value changes, an event is triggered? If so, how can I do this?

It is possible that I could misunderstand how the if statement works! Any help would be greatly appreciated.

+68
c # event-handling visual-studio windows-phone-7 silverlight
Apr 30 2018-11-11T00:
source share
4 answers

It seems to me that you want to create a property.

public int MyProperty { get { return _myProperty; } set { _myProperty = value; if (_myProperty == 1) { // DO SOMETHING HERE } } } 

This allows you to run some code at any time when the property value changes. You can pick up an event here if you want.

+86
Apr 30 2018-11-11T00:
source share

You can use the setter property to raise an event whenever the field value is changed.

You can have your own EventHandler delegate or you can use the famous System.EventHandler delegate.

Usually there is a template for this:

  • Define a public event with an event handler delegate (which has an argument of type EventArgs).
  • Define the OnXXXXX protected virtual method (e.g. OnMyPropertyValueChanged). In this method, you must check whether the delegation of the event handler is zero, and if you do not call it (this means that one or more methods are attached to the delegation of events).
  • Call this protected method when you want to notify subscribers that something has changed.

Here is an example

 private int _age; //#1 public event System.EventHandler AgeChanged; //#2 protected virtual void OnAgeChanged() { if (AgeChanged != null) AgeChanged(this,EventArgs.Empty); } public int Age { get { return _age; } set { //#3 _age=value; OnAgeChanged(); } } 

The advantage of this approach is that you allow any other classes that want to inherit from your class to change behavior if necessary.

If you want to catch an event in another thread that it raises, you must be careful not to change the state of objects that are defined in another thread, which will cause a cross-thread exception. To avoid this, you can either use the Invoke method for the object for which you want to change its state, to make sure that this change occurs in the same stream that the event was raised, or if you are dealing with Windows Form, can use BackgourndWorker to make something in a parallel stream beautiful and light.

+45
Apr 30 '11 at 15:00
source share

The .NET framework actually provides an interface that you can use to notify subscribers when a property changes: System.ComponentModel.INotifyPropertyChanged. This interface has a single PropertyChanged event. It is commonly used in WPF for binding, but I found it useful in business layers as a way to standardize property change notifications.

As for thread safety, I would put a lock in the setter so that you do not encounter any race conditions.

Here are my thoughts in the code :):

 public class MyClass : INotifyPropertyChanged { private object _lock; public int MyProperty { get { return _myProperty; } set { lock(_lock) { //The property changed event will get fired whenever //the value changes. The subscriber will do work if the value is //1. This way you can keep your business logic outside of the setter if(value != _myProperty) { _myProperty = value; NotifyPropertyChanged("MyProperty"); } } } } private NotifyPropertyChanged(string propertyName) { //Raise PropertyChanged event } public event PropertyChangedEventHandler PropertyChanged; } public class MySubscriber { private MyClass _myClass; void PropertyChangedInMyClass(object sender, PropertyChangedEventArgs e) { switch(e.PropertyName) { case "MyProperty": DoWorkOnMyProperty(_myClass.MyProperty); break; } } void DoWorkOnMyProperty(int newValue) { if(newValue == 1) { //DO WORK HERE } } } 

Hope this will be helpful :)

+30
Apr 30 2018-11-21T00:
source share

just use property

 int _theVariable; public int TheVariable{ get{return _theVariable;} set{ _theVariable = value; if ( _theVariable == 1){ //Do stuff here. } } } 
+8
Apr 30 2018-11-11T00:
source share



All Articles