0

I was trying a code with Event raising. But every time when event should raise, if statement is never true as PropertyChanged is always null.

 public delegate void MyEventHandler(object sender, String change);
 public event MyEventHandler PropertyChanged;

 protected virtual void NotifyPropertyChanged(string propertyName)
 {
      if (PropertyChanged != null) //It is always null here
      {
           PropertyChanged(this, propertyName);
      }
 }

This is how I'm subscribing to this event:

data.facebook.PropertyChanged+=new Facebook.MyEventHandler(Facebook_PropertyChanged);
data.PropertyChanged+= new RootObject.MyEventHandler( data_PropertyChanged);

This is the class

public class Facebook
{
    public Facebook()
    {
        _commentsbox_count = 0;
        _comment_count = 0;
        _click_count = 0;
        _like_count = 0;
        _share_count = 0;
        _total_count = 0;
    }
    double _commentsbox_count;
    double _click_count;
    double _total_count;
    double _comment_count;
    double _like_count;
    double _share_count;

public double commentsbox_count
{
    get {
         return _commentsbox_count; 
    }
    set {
    _commentsbox_count = value;
    NotifyPropertyChanged("commentsbox_count");
    }
}
public double click_count {
    get
    {
        return _click_count;
    }
    set
    {
        _click_count = value;
        NotifyPropertyChanged("click_count");
    }
}
public double total_count {
    get
    {
        return _total_count;
    }
    set
    {
        _total_count = value;
        NotifyPropertyChanged("total_count");
    }
}
public double comment_count {
    get
    {
        return _comment_count;
    }
    set
    {
        _comment_count = value;
        NotifyPropertyChanged("comment_count");
    }
}
public double like_count {
    get
    {
        return _like_count;
    }
    set
    {
        _like_count = value;
        NotifyPropertyChanged("like_count");
    }
}
public double share_count {
    get
    {
        return _share_count;
    }
    set
    {
        _share_count = value;                
        NotifyPropertyChanged("share_count");
    }
}

public delegate void MyEventHandler(object sender, String change);
public event MyEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, propertyName);
    }
}
}

public class RootObject
{
    public RootObject()
    {
        _stumbleUpon = 0;
        _twitter = 0;
        _reddit = 0;
        _pinterest = 0;
        _linkedin = 0;
        _googleplusone = 0;
        _diggs = 0;
        _delicious = 0;
        _buzz = 0;
    }
    public Facebook facebook=new Facebook();
    double _stumbleUpon;
    double _reddit;
    double _delicious;
    double _googleplusone;
    double _buzz;
    double _twitter;
    double _diggs;
    double _pinterest;
    double _linkedin;

public double StumbleUpon {
    get
    {
        return _stumbleUpon;
    }
    set
    {
        _stumbleUpon = value;
        NotifyPropertyChanged("StumbleUpon");
    }
}
public double Reddit {
    get
    {
        return _reddit;
    }
    set
    {
        _reddit = value;
        NotifyPropertyChanged("Reddit");
    }
}
public Facebook Facebook { 
    get 
    { 
        return facebook; 
    }
    set 
    {
        facebook = value;
    }
}
public double Delicious {
    get
    {
        return _delicious;
    }
    set
    {
        _delicious = value;
        NotifyPropertyChanged("Delicious");
    }
}
public double GooglePlusOne {
    get
    {
        return _googleplusone;
    }
    set
    {
        _googleplusone = value;
        NotifyPropertyChanged("GooglePlusOne");
    }
}
public double Buzz
{
    get
    {
        return _buzz;
    }
    set
    {
        _buzz = value;
        NotifyPropertyChanged("Buzz");
    }
}
public double Twitter
{
    get
    {
        return _twitter;
    }
    set
    {
        _twitter = value;
        NotifyPropertyChanged("Twitter");
    }
}
public double Diggs
{
    get
    {
        return _diggs;
    }
    set
    {
        _diggs = value;
        NotifyPropertyChanged("Diggs");
    }
}
public double Pinterest
{
    get
    {
        return _pinterest;
    }
    set
    {
        _pinterest = value;
        NotifyPropertyChanged("Pinterest");
    }
}
public double LinkedIn
{
    get
    {
        return _linkedin;
    }
    set
    {
        _linkedin = value;
        NotifyPropertyChanged("LinkedIn");
    }
}
public delegate void MyEventHandler(object sender, String change);
public event MyEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, propertyName);
    }
}

}

This is how am initializing the data

RootObject data = new RootObject();

What is the possible reason for this?

Update

Ok i figured this out. when the values are assigned to data, a temporary Facebook object is created. it is then copied to data.Facebook (copy constructor). That is why that temporary Facebook object is not subscribed and gives null in the event. As it is copied using copy constructor. no NotifyPropertyChanged is fired in data.facebook. Now i have narrowed this down. But i dont know what is the work around for this. Any advice?

Rishabh876
  • 3,010
  • 2
  • 20
  • 37

1 Answers1

0

This is possible only if you add an event handler to another instance of your class. Check if there are other instances?

Also why don't you use inbuilt INotifyPropertyChanged interface and create your own delegate for this?

EDIT:

As I see in your code you create instance of the Facebook class:

Facebook facebook = new Facebook();

But you don't subscribe for PropertyChanged event of the instance.

Moreover you allow to other devs to replace instance of the Facebook class with another here:

public Facebook Facebook { 
    get 
    { 
        return facebook; 
    }
    set 
    {
        facebook = value;
    }
}  

So it's possible that another non-initialized object could be stored here. I recomend to remove public set.

And last: I don't see how this 2 classes are connected with each other. I see only that Facebook class instantinated in RootObject.

Tony
  • 7,345
  • 3
  • 26
  • 34
  • if i use INotifyPropertyChanged, i wont experience this error? – Rishabh876 Dec 30 '13 at 10:11
  • I guess you will, it was just a tip. I see that you posted more code, so I updated my answer too, please check. – Tony Dec 30 '13 at 10:20
  • i was subscribed to the event for both RootObject and Facebook. i tried to remove set but its still the same. also i made facebook public in RootObject and subscribed to that – Rishabh876 Dec 30 '13 at 17:10
  • Got the reason why this happened, please can you give some advice for a workaround. – Rishabh876 Dec 30 '13 at 21:24