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.
You could do something like this:
Then to subscribe an event, it would be as simple as this: