Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
Declaring Event Handlers in C# is Just Painful, Prove me Wrong Please

So I like VB better than C# for many reasons. Primarily because development experience in VB is built to help productivity. The Development experience in C# is built around being proud of the language rather than solving problems quickly. I could point out many things and have yet to really hear a good argument favoring C#. The only thing C# seems to have an advantage on right now for me (key point there, me) is better support for Lambda expressions.

Creating event handlers is super easy in VB. In the code-behind select events from the top-left dropdown for the Window|Page|Control (you like the usage of the | syntax right), then select the event from the Event list in the top-right drop-down. Then bam! You have a properly signatured event handler for the event that even tells you right there it is a handler for the event in question. Man that second part is so useful when you are reading through code to know what that method is responding to.

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Now in C# you…..well you are stuck going back to the design surface of your form. Opening the Properties Window for the Form|Control|Page you are working with, then clicking the Event button in the Properties Window. Then finding the event you want and typing in a name for the method you want to use and it will then take you directly to the method once you leave focus on that event, even if you want to create another event handler for another event. I find this process very tedious and time consuming. It is also not very natural in my opinion. If I am working in the code I do not want to leave what I am doing to go back to the design surface.

I Twittered my delima and got a couple of responses telling me how easy it is, EventName TAB TAB for example. This does nothing useful, just creates a Type name for something I was not even thinking about usually.

 

I was also told something about snippets, and let’s face it C# has no clue about the use of snippets, there are like 20 or 30 and they help you do things like create if statements. VB has like a zillion out of the box and you can do things like call the Mars rover if you want (that is a sarcastic joke).

So with this I ask, can you tell me how to do this very simple and useful task in C#? I have C# books, I even have the Chris Sells WinForms book and yet nothing I can find to help with the common task.

I say this not a chest thumper for VB, but as someone who does work in both languages. Yes I am primarily VB and honestly that is because in Visual Studio it really just helps you write code and does not force you to clutter your brain cells with remembering stuff that does not help you solve the customer problems. So make C# a more pleasant experience for me and anyone else who wants to explore C# as a .NET language. This is something that has really bugged me for a while and I have asked for help, to no avail. I have searched and searched and nothing. So help me understand why this is so cumbersome and how to make it not cumbersome.

Share this post :
Posted: Monday, July 07, 2008 6:16 PM

by Chris Love
Filed under: ,

Comments

Keith Elder said:

Wanting to help a friend desperately in need I threw this together for you my brother. http://keithelder.net/blog/archive/2008/07/07/Creating-Event-Handlers-in-C-The-Definitive-Guide.aspx Enjoy.
# July 7, 2008 7:09 PM

Dave Herren said:

You seem to have misunderstood the Tab-Tab functionality. If you type "button1.DoubleClick +=" in C# you will be prompted with "new System.EventHandler(button1_DoubleClick); (Press Tab to Insert)". If you press tab the code is inserted and you are now prompted "Press Tab to generate event 'button1_DoubleClick' in this class". Pressing Tab again generates the following: void button1_DoubleClick(object sender, System.EventArgs e) { throw new System.NotImplementedException(); } Admittedly the vb.net way of generating events is a little more convenient, but that is a difference in the IDE implementations, not a difference in languages. I remember reading that the two development teams worked separately on their IDEs. Clearly the vb.net team pulled their event management model from the old vb6 IDE and of course the C# team came up with their own. Personally I only missed the functionality for about a week in 2002 until I got used to the C# sharp way. It's a minor feature and not a reason to choose between one language and another. I learned programming on VB 4/5/6. I spent a month or two in VB.net and then switched to C# and have never looked back. C# is a clean, structured language that lends itself to maintainable coding and it's physically painful now when I have to maintain VB code.
# July 7, 2008 10:24 PM

Andrew Myhre said:

Sorry, what I meant was, in your *code-behind*: Page_Init(object sender, EventArgs e) { MyControl.OnLoad = [tab] [tab] } Try that. You should see cool intellisense things happening as soon as you type [equals] [space]. But I totally agree that the GUI method of adding event handlers in design view is cumbersome. Is it different in VB.Net?
# July 8, 2008 2:44 AM

Dimitris-Ilias Gkanatsios said:

Hi, in C# you can easily override the method that raises the event handler (e.g. the Load event is raised by an OnLoad method), and you can just as easy "handle" the event. After all, I find that it makes more sense to override a method and do whatever you need to do there, instead of handling the event that is raised by the *same* class.
# July 8, 2008 4:58 AM

Speednet said:

Chris, I completely agree. Just the Handles syntax alone is far easier. I find the code with Handles much easier to read and understand than C#'s event handling. The same can be said of most of Visual Basic's syntax. It's easier to read and understand quickly. I don't think having curly braces instead of words to open and close loops and conditionals makes C# and "cleaner". It reduces the number of characters in the file, at the cost of readability. But these days nobody really cares about how much physical space a file takes up -- 1K or 2K, it doesn't matter. Besides, C# coders love to put loads of comments in there, perhaps to make up for the less expressive syntax.
# July 8, 2008 8:11 AM

mharr said:

100% agree with you, Chris.  Although my biggest peeve with C# is case-sensitivity.  There is just no good logical explanation why "myvar" does not mean "MyVar" (and I've heard all the arguments from C# bigots, but their arguments fall neither in the the "good" nor the "logical" category).

Dimitris: you can easily override event handlers in VB also, just as easy and exactly the same as C#.  But for the 95% of the time you are coding event handlers, it is just easier to do in VB.

And that is the overriding characteristic for VB. It's just easier to get things done.

# July 8, 2008 9:39 AM

Joe Chung said:

Characterizing people who disagree with you as bigots is ironic.
# July 8, 2008 11:20 AM

Dimitris-Ilias Gkanatsios said:

Andrew: I will correct you, it's not { MyControl.OnLoad = [tab] [tab] } but { MyControl.OnLoad += [tab] [tab] } So, for ASP.NET projects, you go to Page_Init and you do Page.MyEvent += [tab] [tab] mharr: I know that vb and C# overriding is same thing, I posted it because, judging from your blog post, you wonder how to handle events in C#. And I try to explain that you do not need to event handle BCL classes' events, 'cause you can easily override their "event-raising" functions. Moreover, concerning case sensitivity, it's an inheritance from C/C++. Is it useful? Of course. Simply, you have a bigger name set to choose for your variable, properties etc. You don't have to remember anything, you have Intellisense and you have your own personal coding style. Anyway, it's a matter of taste, I recommend VB to programming beginners, but I recommend C# to people having C,C++, Java background.
# July 8, 2008 11:29 AM

Dimitris-Ilias Gkanatsios said:

(I can't edit so I'm posting again) About the event handler thing, C# has anonymous methods, missing in VB. So, you don't need to declare a separate method, but you can "anonymously" write it inline. Much of assistance in code readability/maintanability.
# July 8, 2008 11:31 AM

Mike said:

If you learn how to use C# case sensitivity, IntelliSense really shines. As for the C#, Event Handler, when in the properties, just double click the empty space to get an automatically generated event handler. What you describe as a plus for VB.NET which is apparently a carry over from previous versions, irritated me to no end. I hated it. The Handles Me.Load part is also flawed from my perspective. Some event handlers can be made generic and handle more than one event. That is why the first parameter is Sender. I deleveoped in Delphi and then switched to C#. I had 10 months of working in VB.NET which I am glad is over. I find getting things done in C# is much easier than VB. I guess it is what you get used to.
# July 8, 2008 12:00 PM

Speednet said:

@Dimitris-Ilias Gkanatsios: Your characterization of VB being for "beginners" is insulting, and typical of a gearhead (A.K.A., propellerhead, geek, etc.) who believes they are an "expert". A true "expert" recognizes that someone who thinks one certain language or technology is the "best" solution has a lot to learn.
# July 9, 2008 10:44 AM

NAB said:

Well VB is the language of children. Coming from a C/C++ (C being the mother of all languages), I can say without any doubt that C# at least retains some of pride of programming syntax that the earlier languages had. VB should be named "VERY Basic" :P
# August 2, 2008 9:39 AM

Chris Love said:

NAB, sorry you feel that way. I program in both languages and they both have a different mentality. C# being overly concerned with the language and VB being overly concerned with being productive. Those are the two main difference. I learned C++ first, then migrated to VB because it was just easier to do the same things. Declaring events is just one example. ;)

# August 2, 2008 1:33 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS