• 0

"Handles" equivalent in C#


Question

I use VB.Net in my visual programming class and i've grown fond of the Handles keyword to easily reuse code for multiple events, but i prefer the coding style of C# and its equivalents.

I was wondering, if there is anything similar (or as easy as) attaching a single function to multiple events (or multiple events to single functions etc. as much as the Handles keyword does in VB)

Thanks :)

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

There isn't a handles equivalent in C#. You use the += and -= to add and remove handlers.

// add a handler to the click event, called SaveHandler.
// void SaveHandler(object sender, System.EventArgs e)
btnSave.Click += new EventHandler(SaveHandler);

// remove a handler
btnSave.Click -= new EventHandler(SaveHandler);

Generally, you define the handler in the Design Mode. Select the control you want create a handler for, you select the Events panel in the Properties Window, select the event you want to handle, and either double-click to get a default name for the method, or type in a name. You can use the dropdowns in the Events panel to tie multiple events to handlers that match the event's delegate.

Link to comment
Share on other sites

  • 0

but, no, nothing quite as easy.

you see, vb was created for the noobs and the lazy. i actually started out learning with vb. but if you like c# more than vb, you just gotta change completely to c# and never look back. i did, and c# is great

you might also notice that in vbnet, visual studio has a much better view of the code in the sense that you can automatically jump to any unwritten event for any object. in c#, you'd have to manually add the hadnelr and manually remove the handler in either the form's load event or, if you're careful, the initializecomponent method

Link to comment
Share on other sites

  • 0
but, no, nothing quite as easy.

you see, vb was created for the noobs and the lazy. i actually started out learning with vb. but if you like c# more than vb, you just gotta change completely to c# and never look back. i did, and c# is great

you might also notice that in vbnet, visual studio has a much better view of the code in the sense that you can automatically jump to any unwritten event for any object. in c#, you'd have to manually add the hadnelr and manually remove the handler in either the form's load event or, if you're careful, the initializecomponent method

So let me get this straight: because C# is so much more difficult to use and unintelligible it's a better language. I suggest you rethink this comment - this is clearly the opinionated ramblings of an uninformed snob. It would be nice if you could put a handles like clause in the actual code in C# because then you wouldn't have to go to the XML to see which routine pertains to which control and event.

In this regard VB is so much better than C#. What other ways is VB better? Oh, allow me - it's written in English rather than gobledigook. Ianmac45 has the audacity to call VB programmers "lazy" when his precious language is written for two-finger typists - yes, it's so much more lazy to type "end sub" instead of "}" or "andalso" instead of "&" or "orelse" instead of "|" or "inherits parentclass" instead of ":parentclass" - shall I go on?

As for it being a language for "noobs" allow me to say that it's easy to spot the so-called "noob" - they're the ones that use the term "noob." I've been programming for over twenty years (hardly a "noob") in a multitude of languages and I can say from extensive experience that the only thing stupider than C and all its flavours are the people who claim it's superior.

But the short answer is, no: you can't put a handles clause in C#.

Link to comment
Share on other sites

  • 0

I disagree with you Ianmac45. Basic has different roots then C-style languages, but that in no way means that they were designed for "noobs". The VB.NET Language has some great features, nearly on par with the functionality of C#. Its my preference to use C#. I like the language style, I find it easy. But in reality, I can pretty much acheive anything I can do in C#, but in VB.NET too.

Are you saying the millions of developers who use VB.NET are all "noobs"?

Link to comment
Share on other sites

  • 0

It seems to me like VB and the Visual Studio VB environment were created with small, hobbyist projects in mind, for two reasons : one, verbose, english-like syntax; two, background compilation. When you're never dealing with lots of code and have a bit of trouble understanding 10 lines of it, it sure is reassuring to read If (a = 0 AndAlso b > a) ... End If. But all those words get in the way of making out the general structure. C-based languages use symbols instead of words because they are easier to spot in a page full of code. " || " is a lot more visually striking than " OrElse " which looks just like every other word out there.

Also, background compilation is very nice for small projects, but many have reported bad performance working with large VB projects because of it. (although these news are getting old and VS2008 might have made this a non-issue).

"For beginners and hobbyist" is also the image Microsoft tries to give of VB : http://www.microsoft.com/Express/

VBExpress : Productivity that is ideal for first time or casual Windows programming.

C#Express : A great combination of power and productivity for the Windows developer.

VC++Express : Horsepower with a finer degree of control than other Express Editions.

Note the use of the term "great" reserved for their pet, C#. :p

That's not to say that VB can't be used on large-scale projects, but that it isn't as much the focus of the language.

Link to comment
Share on other sites

  • 0
So let me get this straight: because C# is so much more difficult to use and unintelligible it's a better language. I suggest you rethink this comment - this is clearly the opinionated ramblings of an uninformed snob. It would be nice if you could put a handles like clause in the actual code in C# because then you wouldn't have to go to the XML to see which routine pertains to which control and event.

In this regard VB is so much better than C#. What other ways is VB better? Oh, allow me - it's written in English rather than gobledigook. Ianmac45 has the audacity to call VB programmers "lazy" when his precious language is written for two-finger typists - yes, it's so much more lazy to type "end sub" instead of "}" or "andalso" instead of "&" or "orelse" instead of "|" or "inherits parentclass" instead of ":parentclass" - shall I go on?

As for it being a language for "noobs" allow me to say that it's easy to spot the so-called "noob" - they're the ones that use the term "noob." I've been programming for over twenty years (hardly a "noob") in a multitude of languages and I can say from extensive experience that the only thing stupider than C and all its flavours are the people who claim it's superior.

But the short answer is, no: you can't put a handles clause in C#.

Strong first post. I applaud your defense towards the original ignorant comment touting VB to be for "noobs", though I respectfully disagree that VB is 'better' than C# (specifically syntactically). But I don't want to start a language war here. After all, we are all .NET, can't we all just get along! :D

Link to comment
Share on other sites

  • 0

The fact is, fellas that under the hood, both languages are broken down to the same IL code.  The syntax is strictly for our "ease" of use.  
"Dim MyInt as Integer "  and int MyInt = new Int() both break down into the same IL code:

http://stackoverflow.com/questions/5746873/where-and-why-use-int-a-new-int

 

 That being said, I have a question... If VB can put an "End If" after an if statement when you hit the return key, why can't C# put in two Parens and two curly braces and put the cursor between the two parens?  Same goes for that semi-colon.  I guess it depends if you want electric windows or crank handles, eh?  Long story short, it's just a matter of preference.  Oh, and a shop doesn't have to be a just C# or just VB shop either.  I and my cohort at work use both and have both in the same web app and they play nicely together.  The machine doesn't care what syntax you use.   To it, it's all just the same IL being compiled.  

Peace.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.