Event Handlers (+5 VB.Net)

Creating event handlers is a pain in the butt. That is why it is so important for the IDE to do it for you. Here is how it is automatically done in VB.Net and C#.

VB.Net:

In VB.Net, creating an event handler is as easy as selecting the member that has the event and then selecting the event.

C#:

C# also has a shortcut. You can go to the class constructor and type the event reference and then “+=<tab><tab>”.

Comparison:

You can see that automatic event handler code generation is much easier in VB.Net. What you can’t see from the screenshots is how much the background compiling helps in the VB.Net case. In order to generate event handler code in C#, you have to successfully compile first. VB.Net does this compiling in the background, and it does not matter if there are errors in your code.

Finding Event Handlers

So what happens when you need to find an event handler quickly?

VB.Net

C#

Comparison

In VB.Net, the exact sequence can be used again. All members in the file that have events are listed in the drop down, and the events that have handlers are bolded. In C#, you will need to rely on naming conventions, and either do a search for the method name or else find it in the method drop down.

It is possible that just because you find the name in the C# drop down with your predefined naming convention, that does not mean that the event handler was actually created (e.g. the add handler line was taken out of the class constructor or it was remarked to do some debugging, and never uncommented).

Removing Event Handlers

To remove an unneeded event handler, half the work is needed in VB.Net. In VB.Net, you only need to delete the automatically created method. In C#, you need to delete the automatically created method and then go to the constructor and delete the line that you typed.

3 comments so far

  1. hobbit on

    Most c# developers would not do the event-handler generation the way that you demonstrated. If it’s for a component, you should be adding from the properties/events tool window so that the event will

    1) be automatically generated for you
    2) so that the event-method binding will be put in the .designer.cs file where it belongs.

    VB.net still wins a point for letting you do it from the code window instead of switching over to the properties window…but it certainly doesn’t win by 5🙂

  2. click n ship on

    The idea is to insert the smaller video tape into the adapter, now you can play the VHS-C – inside the adapter – in a standard VHS tape player.There are actually two types of adapters – manual and battery operated. c++

  3. hyde park resort london on

    You will need to encompass the sort of thing covered here.
    The Fireplace Mall maintains a library of excellent instructional articles on wood-burning
    fireplaces and y hyde park hotel australia. You can’t have an inspector going to every factory every month. The safety committee will review work areas to determine ways to do jobs easier and more safely. And by educating them further on the importance of their role. For ordinary people, they can have their own websites to make it easy to escape out of a fire.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: