Problem raising Event with INotifyPropertyChange (ADD and Remove )New in 2.0?Need help

Hi

I am trying to understand how you raise event when you implement INotifyPropertyChange interface explicitly

I am new to c# (used to program in vb.net) so bear with me.
What I am trying to do is that only when a property changes I raise an event


 
in .net 1.1 you Would do the following



 public class Employee : INotifyPropertyChanged
 {
  public event PropertyChangedEventHandler PropertyChanged;
  private string mLastName;

  public string LastName
  {
   get
   {
    return mLastName;
   }
   set
   {
    if (mLastName != value)
    {
     mLastName = value;
     OnPropertyChanged("LastName");
    }
   }
  }

  private void OnPropertyChanged(string propName)
  {
   if (PropertyChanged != null)
   {
    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName));
   }
  }
 

 


which still works on 2.0 however if I as said you implement the
INotifyPropertyChange explicity VS 2005 will generate the following



class Employee2 : INotifyPropertyChanged
 {
  private string mLastName;

  public string LastName
  {
   get
   {
    return mLastName;
   }
   set
   {
    if (mLastName != value)
    {
     mLastName = value;
     ///OnPropertyChanged("LastName");
    }
   }
  }

    #region INotifyPropertyChanged Members

  event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
  {
   //HOW DO YOU USE THESE 2 METHODS
   add { throw new Exception("The method or operation is not implemented."); }
   remove { throw new Exception("The method or operation is not implemented."); }
  }

  #endregion

 
 




My questions
How do you use the "ADD and Remove method in the event generated
HOw do you raise the event in the property


Thanks a lot for your help




Answer this question

Problem raising Event with INotifyPropertyChange (ADD and Remove )New in 2.0?Need help

  • mjhoagland

    The easiest way to explicitly implement INotifyPropertyChanged would be to create a private backing delegate and pass the 'add' and 'remove' on to it. To raise the PropertyChanged event, you would just invoke the private backing delegate like normal.

     


    private PropertyChangedEventHandler propertyChanged;
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { this.propertyChanged += value; }
        remove { this.propertyChanged -= value; }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.propertyChanged != null)
        {
            this.propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

     

    The C# Programming Guide on MSDN also has an article on this: "How to: Implement Two Interfaces that Have an Event with the Same Name."



  • JAlexandrian

    Thank you very much kevin that is exacly what I was looking for.
    I searched for ages on google,I must be keying the wrong words.

    Gabriel



  • Problem raising Event with INotifyPropertyChange (ADD and Remove )New in 2.0?Need help