I
I
iRumba2015-06-04 09:13:54
JavaScript
iRumba, 2015-06-04 09:13:54

How to handle an event without accepting parameters?

Retreat.
Folks, from the delegate documentation I've read, it's not clear to me why this line compiles?
Object.Event += delegate { };
Now on topic.
I need to execute the same method on different events. This can be a mouse click, pressing or releasing a keyboard button, some kind of own events. There are many such methods. And I don’t want to write a handler for each event that takes the necessary parameters (I don’t need parameters, they don’t interest me) and just launches the necessary method. I just want to bind an event to a method without parameters, and I want to do it dynamically.
Tell me how to do this? Or tell me another solution for my problem.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Bhudh, 2018-08-05
@andrew-corput

if (t.total <= 0) {
    clearInterval(timeinterval);
    clock.innerHTML = '<p>Готово</p>';
}

E
Espleth, 2015-06-04
@Espleth

Google Action and Func, and lambda expressions might also come in handy.

A
Artyom, 2015-06-04
@artem_b89

First, it is better to take and read a smart book, Richter, for example, everything is written there why and how.
Even in the error itself it is written that you need to subscribe to events like this +=, and unsubscribe -=.

class Program
    {
        static void Main(string[] args)
        {
            new TestClass();
            Console.ReadLine();
        }
}
class TestClass
    {
        public delegate void SampleEventHandler();
        event SampleEventHandler onCreate;

        public TestClass()
        {
            onCreate += delegate { Console.Write("Hello "); };
            onCreate += () => { Console.Write("World "); };
            onCreate();
        }

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question