Simple question about += and -= operators with events

  • Thread starter Thread starter minimega
  • Start date Start date
M

minimega

Hello to all NG.

Using events in C# I've the following code:

MyObject myObject = new MyObject();
myObject.Click += new MyObject.ClickHandler(ClickFunction);

so, everytime myObject class raises a new click event, I can process
that event in my other class in ClickFunction function.

But if I want to remove the handling of that event I must use the -=
operator like:

myObject.Click -= new MyObject.ClickHandler(ClickFunction);

Is this right? Or if I .Dispose() the class the event handler is
disposed automatically?

However, why can't I use the following sintax to remove the event
handling?

myObject.Click += null; or myObject.Click -= null;

Why must I create a new istance of MyObject.ClickHandler(ClickFunction)
if I have only to remove the event handling from the class?

If I use null instead "new MyObject.ClickHandler(ClickFunction)" it
seems that the event handling doesn't stop and the event is always
fired. If in the ClickFunction there are references to objects that are
just disposed (for example after the class was unloaded) I get a
NullException error. But how can a event handler callback a function
(ClickFunction) in a unloaded instance of a class? If there are active
references to events handler and I .Dispose() the class, that class is
really disposed and all recferences set to null, or the class keep
alive until I remove _manually_ the event handling with myObject.Click
-= new MyObject.ClickHandler(ClickFunction)?

To better understand:
I've the myStatistics class that raises Updated() event every minute,
sending information about last minute data to all the classes who have
"attached" that event. I create a new istance of myForm class that add
a event handling of myStatistics class using the += operator, to get
updated data every minute. If I .Dispose() myForm class without
manually remove the event handling with -= operator, the myStatistics
class raises always the Updated() event to myForm class (just
..Disposed)?

Can anybody explain me this behavior?

Thanks,
Massimo
 
Why would you think that adding null as a handler would remove an old one?
An event can have multiple subscribers - so multiple classes can use the +=
syntax to add to the same event, and the event will be dispatched to thenm
all. The subscribers essentially get added to a stack of pointers. If you
no longer want notifications, you must remove that pointer from the stack
(-=), not add another invalid pointer.

Another way to look at it is purely mathematically. Let's say we have an
integer n:

int n = 0;

We then add 1 to it:

n += 1;

If you want to remove 1 from it later, which would you do?

n -=1;
(or)
n += null;


-Chris
 
Hi Chris,
thanks for the explain. I'm coming from VB6 and I'm using C# from 4
month, so I'm not so good with new C# and .NET language program.

I don't understand the utility of subscribe more than once the event
from the same calling object (to get two same event instead one?).
However, in that case, if I subscribe two time the same event, and I
want to remove the first subscribed instance of that event, how can C#
understand what instance of the two has to remove if the syntax is
equal for both (-= new delegate())? I know that the first or the second
makes no difference, but if C# give the possibility to add more than
one event handler, I think it must also give the possibility of remove
_that_ specific instance of event handler, am I wrong?

After this, a new question:
if a child form subscribe a parent form's event, when the child form
closes I must unsubscribe the event handler with -= new delegate... If
I don't remove the reference the child form "keeps alive" and receive
the events from the parent form also if the class was disposed and set
to null by the parent form. Why this happens? I must remember myself
that all the subscribed events must be unsubscribed in Form_Closed or
..Dispose event, and if I forgot one the class isn't unloaded and remain
in memory. There isn't a way to let the unloading class to dispose
automatically any reference to other objects? Hope to has been clear.

In VB6 all this mechanism of events and object references is really
more simple than in C#.

Thanks,
Massimo
 
Back
Top