11

Let's just say that I have:

public Boolean booleanValue;

public bool someMethod(string value)
{
   // Do some work in here.
   return booleanValue = true;
}

How can I create an event handler that fires up when the booleanValue has changed? Is it possible?

Tilak
  • 30,108
  • 19
  • 83
  • 131
Arrow
  • 2,784
  • 8
  • 38
  • 61

6 Answers6

14

Avoid using public fields as a rule in general. Try to keep them private as much as you can. Then, you can use a wrapper property firing your event. See the example:

class Foo
{
    Boolean _booleanValue;

    public bool BooleanValue
    {
        get { return _booleanValue; }
        set
        {
            _booleanValue = value;
            if (ValueChanged != null) ValueChanged(value);
        }
    }

    public event ValueChangedEventHandler ValueChanged;
}

delegate void ValueChangedEventHandler(bool value);

That is one simple, "native" way to achieve what you need. There are other ways, even offered by the .NET Framework, but the above approach is just an example.

e_ne
  • 8,340
  • 32
  • 43
8

INotifyPropertyChanged is already defined to notify if property is changed.

Wrap your variable in property and use INotifyPropertyChanged interface.

Tilak
  • 30,108
  • 19
  • 83
  • 131
3
  1. Change the access of the BooleanValue to private and only allow changing it through one method for consistency.

  2. Fire your custom event in that method

.

private bool _boolValue;

public void ChangeValue(bool value)
{
    _boolValue = value;
   // Fire your event here
}

Option 2: Make it a property and fire the event in the setter

public bool BoolValue { get { ... }  set { _boolValue = value; //Fire Event } }

Edit: As others have said INotifyPropertyChanged is the .NET standard way to do this.

lahsrah
  • 9,013
  • 5
  • 37
  • 67
2

Perhaps take a look at the INotifyPropertyChanged interface. You're bound to come across it's use again in future:

MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
2
CallingClass.BoolChangeEvent += new Action<bool>(AddressOfFunction);

In your class with the bool property procedure:

public event Action<bool> BoolChangeEvent;
public Boolean booleanValue;
public bool someMethod(string value)
{
   // Raise event to signify the bool value has been set.
   BoolChangeEvent(value);

   // Do some work in here.
   booleanValue = true;
   return booleanValue;
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
1

No it is not possible* to get notified about for changes in value of a variable.

You can achieve almost what you want by making the value to be a property of some class and fire events on change as you wish.

*) if your code is debugger for a process you can make CPU to notify you about changes - see data chage breakpoints in Visual Studio. This will require at least some amount of native code and harder to implement correctly for manged code due to hance of objects to be moved in memory by GC.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179