How to pass an event to a method?
Asked Answered
T

9

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 -=

Travesty answered 1/4, 2010 at 12:27 Comment(3)
@Petar Minchev I don't know why... You were first and helped me. Thank you.Travesty
I've updated question to reflect the issue i 'm steel dealing withTravesty
@tomaszs See my edit. This is because previously the event was declared in the same class where it was invoked.Nocturne
A
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();
    }
}
Anselme answered 1/4, 2010 at 12:36 Comment(1)
i've checked it for updated version of my problem and unfortunately it does not work, can you help me?Travesty
T
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.

Travesty answered 1/4, 2010 at 13:20 Comment(1)
This code has a few problems with it. There is a solution is here: msdn.microsoft.com/en-us/library/…Kuomintang
N
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?

Nocturne answered 1/4, 2010 at 12:33 Comment(13)
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 .Travesty
Still have a problem. Sorry for missasking question, not it's pricese and focuses on the situation when i have a errorTravesty
I can't do it because I want attaching to a Click event be in AttachToHandleEventTravesty
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."Samos
@Jamie Ide - Please notice "Edit" at the end of my question please, ok?Travesty
The code isn't semantically correct (you can't pass an event as an argument) and the rest of the answer wasn't helpful. Heck, if people tried things for themselves this site wouldn't need to exist.Samos
@Petar Minchev - Your answer was wrong for gods sake. Please read carefully my v2 question and answer that i put here and stop making this uncomfortable situation. I did give you a plus point, but I see I need to rethink it again.Travesty
@Jamie Ide - Yeah. You can't pass event but as you can see here: https://mcmap.net/q/340980/-how-to-pass-an-event-to-a-method/… you can using workaround. It's so hard to read all answers here? If people would read answers empty discussions like this would not need to existTravesty
After alll thank you everyone for engagement in helping me with this issue. Without your help I couldn't find a good solution. Bye.Travesty
@tomoszs - I downvoted the answer based on the question and answer at the time I ansered it (about 6 hours ago). Before multiple edits and before the answer you linked to. Feel free to downvote my answer if it will make you feel better. This isn't an empty discussion, it's a great example of why voting is anonymous and voters should stay anonymous. I've learned my lesson.Samos
@Jamie Ide If you feel offended by this discussion than sorry. I didn't intend to do that. I did not downvote your answer. More over I think it has a very important part of whole solution so I gave you upvote now.Travesty
I'm not about to be offended by someone who looks like a silly pastel unicorn.Samos
@tomaszs and Jamie Ide: I apologize guys, I was in a bad mood. I will delete my stupid comments above.Nocturne
S
2

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

Samos answered 1/4, 2010 at 12:37 Comment(0)
L
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);
    }
Lippizaner answered 1/4, 2010 at 12:38 Comment(0)
O
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
});
Ossa answered 30/10, 2015 at 8:25 Comment(0)
U
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();
    }
}
Uncivil answered 6/8, 2019 at 11:46 Comment(0)
I
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();
    }
Involuted answered 25/5, 2021 at 22:35 Comment(0)
I
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.

Infiltrate answered 17/1, 2023 at 7:52 Comment(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.Infiltrate

© 2022 - 2024 — McMap. All rights reserved.