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 649707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:58:24+00:00 2026-05-13T21:58:24+00:00

The caption is confusing. Let me clarify a bit: I’d like to provide events

  • 0

The caption is confusing. Let me clarify a bit:

I’d like to provide events that depend on a parameter so an observer can decide to receive events if something happens to a specific “id”. It could look like this:

public event EventHandler Foo (string id);

I’m aware that this syntax is wrong in .NET 3.5, and I’m also aware that this idea introduces additional problem (for instance, how do we manage unsubscription?).

How should I circumvent this issue? I thought about using something like:

public EventHandler Foo (string id);

which is at least legal syntax and could work, but it still does not look very great to me.

Edit: I’m not asking about passing arguments to the callback function. My idea is more like this:

class Bleh
{
    public event EventHandler Foo (string index);

    private void RaiseEvents() // this is called by a timer or whatever
    {
        Foo["asdf"] (this, EventArgs.Empty); // raises event for all subscribers of Foo with a parameter of "asdf"
        Foo["97"] (this, EventArgs.Empty); // same for all "97"-subscribers
        // above syntax is pure fiction, obviously
    }
}

// subscribe for asdf events via:
Bleh x = new Bleh ();
x.Foo["asdf"] += (s, e) => {};

Explanation
Since you’re probably wondering why I try to do this, I’ll explain my situation. I’ve got a class that provides positions of certain objects (each of these identified by some ID string).

Instead of providing an event EventHandler<PositionChangedEventArgs> that is raised for ANY positional changes, I’d like to have an event for every object (accessed by an index), so observers can listen to the events for a specific ID only.

  • 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-13T21:58:25+00:00Added an answer on May 13, 2026 at 9:58 pm

    You could do something like this:

    public class Foo
    {
        public class Bar
        {
            public event EventHandler PositionChanged;
    
            internal void RaisePositionChanged()
            {
                var handler = PositionChanged;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }
        }
    
        private Dictionary<string, Bar> m_objects;
    
        public Bar this[string id]
        {
            get
            {
                if (!m_objects.ContainsKey(id))
                    m_objects.Add(id, new Bar());
    
                return m_objects[id];
            }
        }
    
        private void RaisePositionChanged(string id)
        {
            Bar bar;
            if (m_objects.TryGetValue(id, out bar))
                bar.RaisePositionChanged();
        }
    }
    

    Then to subscribe an event, it would be as simple as this:

    Foo foo = new Foo();
    
    foo["anId"].PositionChanged += YourHandler;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm try to implement caption display in html5, something like: video_element.addEventListener('CUE=11ms',show_sub1,false) how can I
I have this caption function that worlds great. If I fill in the alt
Trying to do an image caption but can't figure the this object selector right
Double-clicking a TStaticText on a form copies the caption of that TStaticText to the
I'm trying to use the JWPlayer Caption plugin in an environment that is not
I would like to change the caption from being: Figure 1: ... to From
I would like to know how to change a label's caption when the user
How can i get the color of the caption of the active form in
I have this small script that shows a caption for a img when it
I'd like a context menu on the caption bar right click any tips/samples pref

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.