views:

68

answers:

6

Just moved from VB.NET to C#.

In VB to connect and Event Handler to a Sub we use the Handles clause.

From what it seems, this do not exist in C#.

After creating a simple application with a button I realize that Window Forms Designer automatically created an EventHandler to my button1_Click function (after I double clicked it), in Form1.Designer.cs with this code:

this.button1.Click += new System.EventHandler(this.button1_Click);

But, in VB, the WinForms Designer create the Handles clause in my class, in the function header. So, C# create the default EventHandler in designer file, while VB creates in main class where control resides.

Is this correct? Am I missing something here?

A: 

Yes, this is correct, the designer creates it in the generated *.designer.cs file. You can of course create event handlers manually with the same syntax. This behaviour is achieved by the partial class feature, where you can define parts of a class in different files.

Femaref
+5  A: 

You're right in that there is no analog to the VB.NET handles clause (or the corresponding WithEvents variable decorator) in C#. These only exist in VB.NET as holdovers from classic VB, and they have overhead in their use (the generated IL actually has to monitor everywhere that the variable is set in order to detach and attach all of the functions that handles that event).

If you examine the IL for a VB.NET form or control, you'll actually find that there is a property created for every single WithEvents variable, and the setter takes care of removing all of the actual handlers from the old value (assuming it's non-null) and attaching them to the new value (assuming it's non-null).

The analog for C#'s event handling in VB.NET is the AddHandler statement.

Adam Robinson
@Adam, how can I examine the IL of my code?
RHaguiuda
@RHaguiuda: The easiest way is to open your assembly in Reflector and examine it. Looking at it in C# will give you a better understanding of what's actually going on under the covers.
Adam Robinson
There's an overhead associated with `Handles`, right enough, but most of the time surely it's completely insignificant?
MarkJ
@MarkJ: From a performance perspective, very likely, yes, but having a property (which means two functions) on your class for *every single variable that is `WithEvents`* (all of the controls on a form, for example) can generate a LOT of needless bloat.
Adam Robinson
@Adam But does it make any practical difference? Does it significantly slow the code or increase memory footprint, or does it get in the developer's way?
MarkJ
@MarkJ: It very well could get in the way; while having `WithEvents` on instances that get explicitly disposed (and fall out of scope) with the form, there's nothing requiring that this is the case. The fact that an attached event handler can (rather, *will*) cause an object not to be collected becomes even more of an issue. The `WithEvents` modifier gives even less indication that you might need to worry about it; if you have a `WithEvents` variable, then you must set it to null once your object is no longer needed, otherwise it will not be collected until the target is.
Adam Robinson
If you have so many Handles and WithEvents that it impacts the form's performance, then you have a problem with the form, not the language constructs.
AMissico
@AMissco: My point was not to say that they're horribly bloated and will degrade performance. Obviously, from a practical perspective, the performance impact is negligible. While the garbage collection pitfalls are real, my intent was to point out that `handles` and `WithEvents` are just VB.NET syntactic sugar; you should know what the higher-level language constructs (like lambdas, for example) are actually doing.
Adam Robinson
A: 

Consider += as AddHandler with AddressOf

Andrey
+1  A: 

The VB Handles keyword is just syntactic sugar to basically do the same thing that you see in C#. You could write VB the same way, but why would you?

The C# method (which you could emulate in VB) does allow more flexibility (i.e. calling the same method for two different controls).

David Crowell
@David: The `handles` keyword goes hand-in-hand with the `WithEvents` keyword, which there are no equivalents for in C#.
Adam Robinson
A: 

Everything that is modified into the Form1.cs[Design] will have a code part into Form1.Deisgner.cs This is the way C#.NET knows how to make stuff to appear.

It should have created also in your Form1.cs some code where you actually fill what will be in the event.

Wildhorn
+1  A: 

Hi,

This behavior is absolutely correct and you are not missing anything.

In VB, you can subscribe to an event using Handles keyword or AddHandler and AddressOf operators. Visual Studio uses Handles keyword for designer generated events. Since, the event handlers must be in the code behind file and not in the designer file, you can see them while editing the code behind file. If Visual Studio used AddHandler, it would have inserted that code into InitializeComponent method in the designer code file.

On the other hand, C# has one operator (+=) only to subscribe to events. So, that code goes into the designer file and not in the code behind file to avoid mixing designer generated code with the user code.

Hope this helps!

decyclone