JABE Posted June 5, 2004 Posted June 5, 2004 (edited) What is the equivalent in C# to declaring an object variable WithEvents in VB.Net? 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. Edited June 7, 2004 by Nerseus Quote
Jaco Posted June 5, 2004 Posted June 5, 2004 For backward-compatibility reasons' date=' I suppose.[/quote'] 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). Quote
Administrators PlausiblyDamp Posted June 5, 2004 Administrators Posted June 5, 2004 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? 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). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jaco Posted June 6, 2004 Posted June 6, 2004 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. Quote
Drizzt109 Posted June 7, 2004 Posted June 7, 2004 thanks for the discussion, even if you guys didnt agree! Quote
*Experts* Nerseus Posted June 7, 2004 *Experts* Posted June 7, 2004 @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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
ThePentiumGuy Posted June 7, 2004 Posted June 7, 2004 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... what about a one line if? ;) Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
Leaders Iceplug Posted June 7, 2004 Leaders Posted June 7, 2004 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. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Administrators PlausiblyDamp Posted June 7, 2004 Administrators Posted June 7, 2004 The next version of VS has some nice improvements in this area for C# type if and hit TAB and you will get if (true) { } auto generated, there are similar ones for looping, try...finally blocks etc Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.