WithEvents/Overload in C#

JABE

Junior Contributor
Joined
Jun 24, 2003
Messages
375
Location
Philippines
What is the equivalent in C# to declaring an object variable WithEvents in VB.Net?

Jaco said:
No need for this in C#. (I'm not even sure why VB had it)

Once you've declared a variable of a type which generates events, you can either add handlers programmatically as PlausiblyDamp suggests, or you can add them by clicking on the lightening bolt on the properties tool dialog and the double clicking the appropriate event.

For backward-compatibility reasons, I suppose.
 
Last edited by a moderator:
JABE said:
For backward-compatibility reasons, I suppose.

I don't know why VB *ever* had it - even with it, you could still choose not to respond to events by not coding event handlers, so I'm not sure why VB6 or VB.NET has it. (then again I'm also not sure why VB has a lot of things - such as "Overloads", which do absolutely nothing).
 
WithEvents was originally there to allow you to declare variables that could raise events and allow you to handle those events e.g. Recordset as VB6 and earlier had no easy way in the language to dynamically wire-up event handlers at runtime.
In VB.Net the AddHandler / RemoveHandler remove the need for WithEvents but it is still there for backwards compatabiliity.

The Overloads keyword does actually server a purpose - in the following code snippet it isn't require for the two methods in the class Base - if you provide the keyword on one of the methods though it must be supplied on all the overloaded 'Test' methods.
However in the Derived class it will be used to make our intentions clearer - are we adding an overloaded version or hiding the original implementation?

Visual Basic:
Public Class Base

	Public Sub Test(ByVal i As Integer)

	End Sub

	Public Sub Test(ByVal s As String)

	End Sub
End Class

Public Class Derived
	Inherits Base

	Public Overloads Sub test(ByVal b As Boolean)
		'Public Shadows Sub Test(ByVal b As Boolean)

	End Sub

End Class
using the overloads then a variable of type derived will have 3 overloaded Test methods - using the version with shadows it would only have 1 (accepting a boolean).
 
I'm not sure I agree with your reasoning about why "WithEvents" was in VB6. It didn't allow wiring events at runtime - you still wired them at design time, even for "WithEvents" variables. So, it was a useless construct (although VB forced you to type it in order to see the available events in the dropdown menu).

As for Overloads, it is only necessary because Microsoft forces you to type it. Again, there is no reason for Microsoft to have required it - note that C# doesn't require it. Actually, to convert from VB to C#, you simple remove the "Overloads" keyword.
 
@Jaco: Some might argue why almost ANY keywords are necessary. For example, why type "Then" for an "If"? Why not use C++ style curly braces to save on other typing?

I think the WithEvents was an alert to the compiler that it needed to do a lot more work for that variable so that it could handle events (not to mention Visual Studio so that it could, as you pointed out, let you create an event through the drop-downs).

My personal thoughts are that VB6 and below were meant to be very easy to understand and, in that regard, VB was beautiful as a language. It may not have been the fastest to type in, but it gave you a chance to make the code very easy to read (not necessarily understand, which is what the developer should be doing).

Overload is a big necessity, in my opinion. It states clearly that the developer is overloading something existing versus creating a new version of the method. Stating the obvious today makes the code MUCH more readable tomorrow when you don't remember what methods are "new" and which ones are being overridden.

Now why Visual Studio doesn't fill in the parens as soon as you type in "if", I don't know. I can't think of a single time when you'd type in "if" and NOT want the parens there...

-Nerseus
 
I think he's talking about the (), not the {}.
The C# compiler highlights that immediately if there're no ().
Along with several other gems you get after you click Build. :)
 
The next version of VS has some nice improvements in this area for C#
type if and hit TAB and you will get
C#:
if (true)
{

}
auto generated, there are similar ones for looping, try...finally blocks etc
 
Back
Top