Create an event to watch for a change of variable

前端 未结 6 2068
抹茶落季
抹茶落季 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: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.

提交回复
热议问题