meaning of event handler code

B

Beth

in the following:
this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click);

if I saw an equation, such as y +=x;
then y = y+x.

But what is the meaning in the event handler. I realize it is adding an
event handler for the click event of the exit button, but how do i reason
through the 2 step process inferred by the += equation.

Beth
 
D

Daniel O'Connell [C# MVP]

Beth said:
in the following:
this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click);

if I saw an equation, such as y +=x;
then y = y+x.

But what is the meaning in the event handler. I realize it is adding an
event handler for the click event of the exit button, but how do i reason
through the 2 step process inferred by the += equation.

+= with events means add an event handler to the set. It is basically a
specialization of hte += operator to hook up event handlers, unlike the
equation y = y+x, += just specifcally means "hook up this handler to the
event." It calls the add handler for the event. There isn't a Click = Click
+ Handler because it isn't possible to assign to an event from outside of
the class.

If I understand waht you are asking, anyway.
 
N

Nicholas Paldino [.NET/C# MVP]

It goes a little bit beyond that, and has it's roots in how delegates
are handled.

When you attach a delegate to an event handler, the delegate at the end
of the list ends up being assigned the reference to the delegate that is
being attached. Your invocation list is actually a linked list.

So, when you use += to attach a delegate to an event handler, it kind of
makes sense when you expand it now to event = event + new delegate, because
what is happening is that you are taking the invocation list, and adding the
event handler to the end of the invocation list.

Hope this helps.
 
B

Beth

Thank you, that makes sense.
Nicholas Paldino said:
It goes a little bit beyond that, and has it's roots in how delegates
are handled.

When you attach a delegate to an event handler, the delegate at the end
of the list ends up being assigned the reference to the delegate that is
being attached. Your invocation list is actually a linked list.

So, when you use += to attach a delegate to an event handler, it kind
of makes sense when you expand it now to event = event + new delegate,
because what is happening is that you are taking the invocation list, and
adding the event handler to the end of the invocation list.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Daniel O'Connell said:
+= with events means add an event handler to the set. It is basically a
specialization of hte += operator to hook up event handlers, unlike the
equation y = y+x, += just specifcally means "hook up this handler to the
event." It calls the add handler for the event. There isn't a Click =
Click + Handler because it isn't possible to assign to an event from
outside of the class.

If I understand waht you are asking, anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top