WithEvents = ? in C#

C# doesnt really have a one-to-one mapping for the WithEvents. Instead you would programatically wire-up event handlers like...

C#:
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
}
 
Theres no need. You just add an event handler.
Say your variable exposes an event named MyEvent of type System.EventHandler:
C#:
// 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
 
What is the equivalent in C# to declaring an object variable WithEvents in VB.Net?

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.
 
Last edited by a moderator:
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#.
 
pelikan said:
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 anyones looking to get some of that ugly VB code converted to C#, I found a great vb.net to c# converter named Instant C# (www.instantcsharp.com).
 
Back
Top