Create an event to watch for a change of variable

前端 未结 6 2066
抹茶落季
抹茶落季 2020-12-15 11:00

Let\'s just say that I have:

public Boolean booleanValue;

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


        
相关标签:
6条回答
  • 2020-12-15 11:42

    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.

    0 讨论(0)
  • 2020-12-15 11:43
    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;
    }
    
    0 讨论(0)
  • 2020-12-15 11:51

    INotifyPropertyChanged is already defined to notify if property is changed.

    Wrap your variable in property and use INotifyPropertyChanged interface.

    0 讨论(0)
  • 2020-12-15 11:54

    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.

    0 讨论(0)
  • 2020-12-15 11:55

    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

    0 讨论(0)
  • 2020-12-15 11:58
    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.

    0 讨论(0)
提交回复
热议问题