views:

319

answers:

5

My friend is primarily a VB developer and he says time and time again how much more simple it is to code events in VB than C#. My take on the issue is that it probably is easier but if there was not a reason for the added complexity, they probably would have made it just as simple in C#. Can anyone tell me if there is any added flexibility or any ability, in general, that can be done with C# events and not VB.Net events?

+2  A: 

I prefer VB to C# most of the time, but I'm pretty fluent in both.

Off the top of my head there are two places I know where VB.Net makes it easier. One is that you don't ever have to check for null before raising an event. There might be some trade-off there, but I"m not aware of it.

The other is the addition of the Handles keyword. You can declare a full method to handle event and wire it up to the event in one statement. This is a definite advantage for VB, because you could still do everything in long form without that keyword. It's just an extra little piece of syntactic sugar. The only way you can do that in C# is with a lambda expression/anonymous delegate.

The rest of the syntax is pretty much a wash: do you prefer "+=" or "AddHandler"?

Joel Coehoorn
I suppose that trade-off you're mentioning is that you cannot check if event already has event handler attached.
Hugo Riley
+1  A: 

With VB, can't you imply a signature, rather than stating a delegate type?

Public Event OnChange(ByVal Text As String)

I'm not sure it is a good thing, but...

Also - doesn't RaiseEvent handle nulls (non-subscribed events) automatically? Trivial to do, of course.

Marc Gravell
+1, you can. The drawback is that (I believe) the compiler makes a new delegate type behind the scenes every time.
MarkJ
+4  A: 

The only thing that comes to mind for C# is the ability to subscribe to a void returning event (virtually all events) with an anonymous fuction. VB.Net 9.0 only supports Lambda Expressions which returns a value (this is fixed in VB 10.0).

VB has a bit of flexbility not present in C# with regard to events

  • Support for Relaxed Delegates. This allows VB to use event handlers which only need a subset of the parameters in the event type (mostly used with empty parameter functions)
  • The Handles clause makes it much easier to delete designer generate events as opposed to C# where you have to dig through the .Designer.cs file
  • The RaiseEvent keyword makes the null event check problem non-existent in VB.Net
JaredPar
I can't seem to create an event with a return type. Any help on my post would be appreciated: http://stackoverflow.com/questions/2624886/vb-equivalent-of-c-event-creation
Steven
A: 

Your friend is probably used to creating event handlers by selecting the control and event from the two combo-boxes on top of the source code page. Where as in C# you manually code the += handler.

dotjoe
A: 

In C# you can have virtual events and override them in derived class. There is no such thing in VB.NET.

alex