Is there a way to detect a variable change, and then fire an event for it?

I read the article on events tutorial in C#
http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx

But I couldn't make sense of it, since I tried putting it in my program and it didn't work. I was trying it with a boolean value.


Thanks!

Recommended Answers

All 2 Replies

Hi,

No you can't !

.. and I hope your variable isn't public !

Anyway, I think you misunderstood the event communication concept; the way this works is like this: an object does something ( like changing it's state = "changing a variable") and then it notifies another object ( or thread running in another method, same object ...) that it did something and the notified object reacts (eg. it checks the state of the notifier).

You need to use properties and you can. Here is an example class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace daniweb
{
  public class SomeClass : INotifyPropertyChanging, INotifyPropertyChanged
  {
    private string _name;
    private int _weight;

    public string Name
    {
      get { return _name; }
      set 
      {
        if (_name != value)
        {
          SendPropertyChanging("Name");
          _name = value;
          SendPropertyChanged("Name");
        }
      }
    }

    public int Weight
    {
      get { return _weight; }
      set 
      {
        if (_weight != value)
        {
          SendPropertyChanging("Weight");
          _weight = value;
          SendPropertyChanged("Weight");
        }
      }
    }

    private void SendPropertyChanging(string property)
    {
      if (this.PropertyChanging != null)
      {
        this.PropertyChanging(this, new PropertyChangingEventArgs(property));
      }
    }
    private void SendPropertyChanged(string property)
    {
      if (this.PropertyChanged != null)
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(property));
      }
    }

    public SomeClass()
    {
    }

    #region INotifyPropertyChanging Members
    public event PropertyChangingEventHandler PropertyChanging;
    #endregion

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
  }
}

Calling it:

private void button2_Click(object sender, EventArgs e)
    {
      SomeClass sc = new SomeClass();
      sc.PropertyChanging += new PropertyChangingEventHandler(sc_PropertyChanging);
      sc.PropertyChanged += new PropertyChangedEventHandler(sc_PropertyChanged);
      sc.Name = "Daniweb";
      sc.Weight = int.MaxValue;
    }

    void sc_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      Console.WriteLine(e.PropertyName + " is changing");
    }

    void sc_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
      Console.WriteLine(e.PropertyName + " has been changing");
    }

Results in:

Name has been changing
Name is changing
Weight has been changing
Weight is changing
commented: You sir should write a book! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.