Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3493502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:53:25+00:00 2026-05-18T11:53:25+00:00

I might be overthinking this one a little but I could use some help

  • 0

I might be overthinking this one a little but I could use some help in identifying a way/the best way to do the following.

I have an event handler that is attached to an object that is a property of another class. In my event handler I need additional meta-data about the object that caused the event (i.e. the ID of the object it is contained in). From the sender and the event information there is no way to obtain the information I need. My inclination was that this would be a good place to use a captured variable but I’m unsure of my implementation ideas.

So to illustrate in code I have an event handler:

void MyEventHandler(object sender, EventArgs e){
    //Do Stuff here
}

(As a note I’m using base EventArgs here but in my actual implementation the are a specialized subclass and the event is declared using the generic EventHandler)

I am currently attaching it like this:

topObject.SubObject.EventToHandle += MyEventHandler;

I later detatch it like so:

topObject.SubObject.EventToHandle -= MyEventHandler;

I want topObject’s ID when I’m handling the event so I was going to change the MyEventHandler to have the following signature:

void MyEventHandler(int id, object sender, EventArgs e)

and attach the event handler like this:

topObject.SubObject.EventToHandle += (s,e) => MyEventHandler(topObject.ID, s,e);

My concern with that is two fold.

  1. Is there a problem with the scope where the handler will actually disapear without removal once I’m outside the function where this is attached. I’ve seen some wierd errors in the past where event handler disapeared on me when I used lambda expression. Its not all the time, just in some cases. Can anyone enlighten me on what those cases might be so I know when its safe to use the syntax I had.
  2. I can’t remember exactly but I don’t think I can ever have the event handler removed if I use this syntax because the implicit object that is created is not the same.

Because of those two concerns my thought was to create an Action and save the action and use it until I needed to remove the event handler. I did the following:

Action<object, EventArgs> handler = (s,e) => MyEventHandler(topObject.ID, s,e);
topObject.SubObject.EventToHandle += handler;

I get that the action cannot be casted to an event handler. Is there some easy way that I can make this transformation that will still ensure that I can detach the event handler? Am i just over thinking this/ is there a way I’m not seeing right now to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:53:26+00:00Added an answer on May 18, 2026 at 11:53 am

    You can create all nice wrapper functions that wrap existing event-handlers and supply them with object ids, but you will still have to store the resulting delegate explicitly to unsubscribe the event.

    The only nice way I see to do it without ugly wrappers is using reactive extensions. It essentially allows you to transform your event to an IObservable, and then you can apply any operator to the resulting IObservable (for example Select will do the job in your case). But it’s still no that elegant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this might be fairly simple but I cant find my way around... I have
This might seem obvious but I've had this error when trying to use LINQ
This might sound like a little bit of a crazy question, but how can
This might seem like a stupid question I admit. But I'm in a small
This might be on the discussy side, but I would really like to hear
This might be a bit on the silly side of things but I need
This might be an odd question, but when I scale my image in C#
I know there's an easy way of doing this, but my recursion abilities are
Might be the same issue as this previuos question: WCF Proxy but not sure...
Might not be a very consequential question, but... I have a bunch of mostly-static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.