c# - Why is my event handler null? -
i've got quite simple problem gives me headache , i'm wasting time. i've ran out of ideas anyway:) reason can't pass value of property variable event handler. here i've got , me fine won't work:(
any idea why it's not passing actual value of variable? in advance:)
just name suggests, propertyname
should contain property's name, not value. see propertychangedeventhandler , propertychangedeventargs on msdn more details.
as why event handler null
, suspect haven't subscribed it. should have following somewhere in program:
obj.propertychanged += new propertychangedeventhandler(obj_propertychanged); private void obj_propertychanged(object sender, propertychangedeventargs e) { .... }
then when obj.moves
changes obj_propertychanged
called.
i understand confusion, let me give little more explanations. suppose have 2 classes a
, b
, , want b
notified when property of a
changed. can make a
implement inotifypropertychanged, , make b
subscribe propertychanged event of a
, following:
public class a: inotifypropertychanged { private int moves; public int moves { { return moves; } set { if (value != moves) { moves = value; onpropertychanged("moves"); } } } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(propertyname)); } } public class b { private a = new a(); public b() { a.propertychanged += new propertychangedeventhandler(a_propertychanged); } private void a_propertychanged(object sender, propertychangedeventargs e) { .... } }
with above, b.a_propertychanged
called whenever a.moves
changed. more precisely, if b
instance of b
, b.a_propertychanged
called whenever b.a.moves
changed. please note how implementation of setter of moves
differs yours.
Comments
Post a Comment