WithEvents/Overload in C#

JABE

Well-known member
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#. (Im not even sure why VB had it)

Once youve 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 dont know why VB *ever* had it - even with it, you could still choose not to respond to events by not coding event handlers, so Im not sure why VB6 or VB.NET has it. (then again Im 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 isnt 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?

Code:
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).
 
Im not sure I agree with your reasoning about why "WithEvents" was in VB6. It didnt 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# doesnt 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 dont remember what methods are "new" and which ones are being overridden.

Now why Visual Studio doesnt fill in the parens as soon as you type in "if", I dont know. I cant think of a single time when youd type in "if" and NOT want the parens there...

-Nerseus
 
I think hes talking about the (), not the {}.
The C# compiler highlights that immediately if therere 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