3
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by sknake
Featured Replies
  • 1
    sknake 1,622   7 Years Ago

    You need to use properties and you can. Here is an example class: [code=csharp] 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 … Read More

0

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).

1

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
Comments
You sir should write a book!
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.