28

I would like to create a method that takes an event as an argument and adds eventHandler to it to handle it properly. Like this:

I have two events:

public event EventHandler Click;
public event EventHandler Click2;

Now I would like to pass a particular event to my method like this (pseudocode):

public AttachToHandleEvent(EventHandler MyEvent)
{
    MyEvent += Item_Click;
}

private void Item_Click(object sender, EventArgs e)
{
    MessageBox.Show("lalala");
}

ToolStripMenuItem tool = new ToolStripMenuItem();
AttachToHandleEvent(tool.Click);

Is it possible?

I've noticed that this code worked fine, and returned to my project and noticed that when I pass an event declared in my class, it works, but when I pass event from other class it still does not work.

What I get is this error:

The event 'System.Windows.Forms.ToolStripItem.Click' can only appear on the left hand side of += or -=

3
  • @Petar Minchev I don't know why... You were first and helped me. Thank you. Apr 1, 2010 at 12:44
  • I've updated question to reflect the issue i 'm steel dealing with Apr 1, 2010 at 12:51
  • @tomaszs See my edit. This is because previously the event was declared in the same class where it was invoked. Apr 1, 2010 at 13:13

9 Answers 9

29

My original answer was suitable from within the class that defined the event, but you've since updated your question to reflect that you wish to accomplish this from outside the defining class, so I've stricken that.

Only the class that defines an event can refer to the implicit delegate variable that the event uses. From outside that class, you only have access to the add and remove methods, via += and -=. This means that you can't do what you're asking, directly. You can, however, use a functional approach.

class A{
    public event EventHandler Event1;

    public void TriggerEvent1(){
        if(Event1 != null)
            Event1(this, EventArgs.Empty);
    }
}

class B{
    static void HandleEvent(object o, EventArgs e){
        Console.WriteLine("Woo-hoo!");
    }

    static void AttachToEvent(Action<EventHandler> attach){
        attach(HandleEvent);
    }

    static void Main(){
        A a = new A();
        AttachToEvent(handler=>a.Event1 += handler);
        a.TriggerEvent1();
    }
}
1
  • i've checked it for updated version of my problem and unfortunately it does not work, can you help me? Apr 1, 2010 at 12:52
6

I did it like this:

public AttachToHandleEvent(Object obj, string EventName)
{
    EventInfo mfi = obj.GetType().GetEvent(EventName);
    MethodInfo mobj = mfi.GetAddMethod();
    mobj.Invoke(obj, new object[] { Item_Click});
}

private void Item_Click(object sender, EventArgs e)
{
    MessageBox.Show("lalala");
}

ToolStripMenuItem tool = new ToolStripMenuItem();
AttachToHandleEvent(tool "Click");

Thank you all for advice. This solution could not be done without your help.

1
2

It's not possible. You can use a delegate instead of an event if that meets your needs.

2

Just write tool.Click += Item_Click;

Edit: From MSDN "Events can only be invoked from within the class or struct where they (it) are declared". So what you are trying to do is not possible. Could you elaborate more on your needs? Why would you want to pass an event as a parameter?

13
  • 1
    It's not minus from me. Your answer was first and correct one. I've tried longer version of this code and it didn't want to work. I've wrote this question here to ask for help, but when after you answer I've rechecked this simple example from here it occurred to be correct as you wrote. Thanks . Apr 1, 2010 at 12:41
  • 1
    Still have a problem. Sorry for missasking question, not it's pricese and focuses on the situation when i have a error Apr 1, 2010 at 12:53
  • I can't do it because I want attaching to a Click event be in AttachToHandleEvent Apr 1, 2010 at 13:03
  • It was in response to your original answer: "I don't see a problem, the code is semantically correct. Edit: You asked is it possible. Just try it:). It would have taken less time to try it, than to ask here."
    – Jamie Ide
    Apr 1, 2010 at 16:28
  • 1
    I'm not about to be offended by someone who looks like a silly pastel unicorn.
    – Jamie Ide
    Apr 1, 2010 at 20:03
1
    delegate void doIt(object sender, object data);
    event doIt OnDoIt;

    void add(doIt theDel)
    {
        OnDoIt += theDel;
    }

    void doIt1(object a, object b)
    {
    }

    void doIt2(object a, object b)
    {
    }

    void add()
    {
        add(doIt1);
        add(doIt2);
    }
0

Your question suggests that you got some mechanisms wrong: You can't pass events!

You most probably want to pass a function as a parameter, so the calling method will call that other method at some point. In technical terms this is a delegate. I suggest using the already defined Action class. Here's an example snippet:

void MyFunction (string otherArguments, Action onFinished){
    ...
    if (onFinished != null)
        onFinished.Invoke();
}

The nice thing about this is that when calling MyFunction you can declare the Action using the inline syntax:

MyFunction("my other argument", ()=>{
    ///do stuff here, which will be execuded when the action is invoked
});
0
0

I pass functions/methods (instead of events) like this:

class A
{
    public void something()
    {
        var myAction = 
            new Action<object, object>((sender, evArgs) => {
                MessageBox.Show("hiii, event happens " + (evArgs as  as System.Timers.ElapsedEventArgs).SignalTime); 
            });
        B.timer(myAction);
    }
}

class B
{
    public static void timer( Action<object, System.Timers.ElapsedEventArgs> anyMethod)
    {
        System.Timers.Timer myTimer = new System.Timers.Timer();
        myTimer.Elapsed += new System.Timers.ElapsedEventHandler(anyMethod);
        myTimer.Interval = 2000;
        myTimer.Start();
    }
}
0

Giving an update to this question with an object oriented solution.

Instead of using an Action<EventHandler> that registers the event, you could create an object handling that for you

    public class AEvent
    {
        private readonly A aInstance;
        private AEvent(A instance) {
            aInstance = instance;
        }        

        public void Add(EventHandler eventHandler)
            => a.Event1 += eventHandler;

        public void Remove(EventHandler eventHandler)
            => a.Event1 -= eventHandler;

        public EventHandler Invoke => aInstance.Event1;
    }

Then later on use that object like this:

    static void Main(){
        A a = new A();
        AEvent aEvent = new AEvent(A)
        aEvent.Add(handler);
        a.Invoke();
    }
0

One approach I haven't seen here would be to create an object which has delegates for subscribe and unsubscribe. Here is a complete example program.

class Program
{
    private event EventHandler<EventArgs> eventHandler;

    public static void Main(string[] args)
    {
        Program program = new Program();
        Thing thing = new Thing(new EventWrapper<EventArgs>(
            delegate(EventHandler<EventArgs> handler) { program.eventHandler += handler; },
            delegate(EventHandler<EventArgs> handler) { program.eventHandler -= handler; }
        ));

        // events are fired
        program.eventHandler?.Invoke(program, EventArgs.Empty);

        thing.Unsubscribe();
    }
}

class Thing
{
    private readonly Action<EventHandler<EventArgs>> _unsubscribeEventHandler;

    public Thing(EventWrapper<EventArgs> eventHandler)
    {
        this._unsubscribeEventHandler = eventHandler.Unsubscribe;
        eventHandler.Subscribe?.Invoke(OnEvent);
        Console.WriteLine("subscribed");
    }

    private void OnEvent(object? sender, EventArgs e)
    {
        Console.WriteLine("event fired");
    }

    public void Unsubscribe()
    {
        _unsubscribeEventHandler?.Invoke(OnEvent);
        Console.WriteLine("unsubscribed");
    }
}

class EventWrapper<T> where T : EventArgs
{
    public Action<EventHandler<T>> Subscribe { get; private set; }
    public Action<EventHandler<T>> Unsubscribe { get; private set; }

    public EventWrapper(Action<EventHandler<T>> subscribe, Action<EventHandler<T>> unsubscribe)
    {
        Subscribe = subscribe;
        Unsubscribe = unsubscribe;
    }
}

In this example, we created a new class called EventWrapper<T> which wraps delegates for += and -= and exposes them with Subscribe and Unsubscribe methods. The delegates will need to be created by the class which created the event.

1
  • I would like feedback on this answer please. I am still learning .net and if this is an anti-pattern or causes issues I'd like to know about it. Thanks. Jan 17, 2023 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.