WithEvents = ? in C#

C# doesn't 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
}
 
There's 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#. (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.
 
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 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# (www.instantcsharp.com).
 
Back
Top