How to send method name as parameter

Ján Яabčan

I want to create some engine for monogame in c#. My engine have to include button. I want to do it with events. Basically I have it done, but I want to improve it and there began my problems.

  class Button : Component
  {
    ....
    public event EventHandler<EventArgs> Click;
    public void OnClick()
    {
        if (MousePressed() && Click != null)
        {
            Click(this, new EventArgs());
        }
    }
  }

and when I want to add Click event I have to do it in another class:

_buttnon.Click += new EventHandler<EventArgs>(Method);
_buttnon.OnClick();

It works nice, but I want to do it in following way:

_button.RegisterClickEvent(Method);

so I added to the Button class code below:

 public void RegisterClickEvent(XXX method) {
        Click += new EventHandler<EventArgs>(method);
 }

I absolutely have no idea what I have to replace the XXX. I tried:

 public void RegisterClickEvent(void(object, EventArgs)method) {
        Click += new EventHandler<EventArgs>(method);
 }

but it is not working. I tried to search for something on Google, but I do not know what I look for.

Julo

Simple use a delegate:

  public void RegisterClickEvent(EventHandler<EventArgs> method)
  {
    Click += new EventHandler<EventArgs>(method);
  }

or

  public void RegisterClickEvent(EventHandler method)
  {
    Click += new EventHandler<EventArgs>(method);
  }

This works (at least in VS2015, perhaps there is another way in previous (before 2010, since there were some changes with delegates).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to send Column Name as parameter in Named Query

From Dev

How to send only 1 parameter with get method?

From Dev

How to send parameter with API in get method

From Dev

@WebParam on a parameter of interface method - how to get name?

From Dev

How to set a class Name as a method's parameter?

From Dev

How to pass a method name as a parameter in Java

From Dev

Send a method implementation as parameter

From Dev

How to dynamically pass name of calling method as a parameter in the same method?

From Dev

How to pass in a method name of a particular class as a parameter of another method

From Dev

how to make guzzle to send parameter without its name?

From Dev

How to send request parameters with same parameter-name

From Java

Getting the name of a method parameter

From Java

Parameterized Junit class - How to send an ArrayList as a parameter to a method()

From Dev

I am trying to send File as a parameter to a method but dont know how?

From

How to send any structure as parameter to a method and return the structure?

From Dev

how to send parameter in post method to api using javascript

From Dev

How is send a parameter to page object method and verify its existence?

From Dev

Is it Safe to Send a Table Name in as Parameter

From Dev

Send call back method as parameter

From Dev

Java send parameter to method paint

From Dev

JavaScript Send a object method as a parameter

From Dev

How to pass object name value pair as a parameter to the method?

From Dev

How to test an method that has like parameter the mock name class

From Dev

How to get the method name used in lambda that was passed a parameter

From Dev

PHP Send method call as parameter for method

From Dev

How to send a parameter to the controller?

From Dev

How to send parameter by view

From Dev

how to send datatype as a parameter

From Dev

Invoke delegate method name with parameter

Related Related

  1. 1

    How to send Column Name as parameter in Named Query

  2. 2

    How to send only 1 parameter with get method?

  3. 3

    How to send parameter with API in get method

  4. 4

    @WebParam on a parameter of interface method - how to get name?

  5. 5

    How to set a class Name as a method's parameter?

  6. 6

    How to pass a method name as a parameter in Java

  7. 7

    Send a method implementation as parameter

  8. 8

    How to dynamically pass name of calling method as a parameter in the same method?

  9. 9

    How to pass in a method name of a particular class as a parameter of another method

  10. 10

    how to make guzzle to send parameter without its name?

  11. 11

    How to send request parameters with same parameter-name

  12. 12

    Getting the name of a method parameter

  13. 13

    Parameterized Junit class - How to send an ArrayList as a parameter to a method()

  14. 14

    I am trying to send File as a parameter to a method but dont know how?

  15. 15

    How to send any structure as parameter to a method and return the structure?

  16. 16

    how to send parameter in post method to api using javascript

  17. 17

    How is send a parameter to page object method and verify its existence?

  18. 18

    Is it Safe to Send a Table Name in as Parameter

  19. 19

    Send call back method as parameter

  20. 20

    Java send parameter to method paint

  21. 21

    JavaScript Send a object method as a parameter

  22. 22

    How to pass object name value pair as a parameter to the method?

  23. 23

    How to test an method that has like parameter the mock name class

  24. 24

    How to get the method name used in lambda that was passed a parameter

  25. 25

    PHP Send method call as parameter for method

  26. 26

    How to send a parameter to the controller?

  27. 27

    How to send parameter by view

  28. 28

    how to send datatype as a parameter

  29. 29

    Invoke delegate method name with parameter

HotTag

Archive