Drizzt109 Posted June 4, 2004 Posted June 4, 2004 What is the equivalent in C# to declaring an object variable WithEvents in VB.Net? Quote
Administrators PlausiblyDamp Posted June 4, 2004 Administrators Posted June 4, 2004 C# doesn't really have a one-to-one mapping for the WithEvents. Instead you would programatically wire-up event handlers like... private void Form1_Load(object sender, System.EventArgs e) { DataSet ds = new DataSet(); ds.MergeFailed+=new MergeFailedEventHandler(ds_MergeFailed); } private void ds_MergeFailed(object sender, MergeFailedEventArgs e) { //event handler here } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted June 4, 2004 *Experts* Posted June 4, 2004 There's no need. You just add an event handler. Say your variable exposes an event named MyEvent of type System.EventHandler: // This is in form_load for example MyType var = new MyType(); var.MyEvent += new System.EventHandler(this.MyHandler); // this is outside form_load, the event handler: private void MyHandler(object sender, System.EventArgs e) { // handle var.MyEvent here. } -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
Jaco Posted June 4, 2004 Posted June 4, 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. Edited June 7, 2004 by Nerseus Quote
pelikan Posted June 10, 2004 Posted June 10, 2004 poor VB has legacy problems which stretch back into the mists of time when B.G. was working out of a garage. Learn a clean elegant syntax - C#. Quote IN PARVUM MULTUM
Jaco Posted June 10, 2004 Posted June 10, 2004 poor VB has legacy problems which stretch back into the mists of time when B.G. was working out of a garage. Learn a clean elegant syntax - C#. Amen brother! BTW: If anyone's looking to get some of that ugly VB code converted to C#, I found a great vb.net to c# converter named Instant C# (http://www.instantcsharp.com). Quote
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.