Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted

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
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Experts*
Posted

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

"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
Posted (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 by Nerseus
Posted
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#.
IN PARVUM MULTUM
Posted
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).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...