Home c# Callback Call

Callback Call

Author

Date

Category

How to make a Callback Call in C #?


Answer 1, Authority 100%

everything is simple:

C++:

void do_action (int & amp x, void (* f) (int & amp;)) {f (x) ; }
int i = 5; do_action (i, twice);
Void Twice (int & amp; x) {x = 2 * x; }

C #:

Public Delegate F (Ref int x); // How would Typedef
Void Doaction (Ref int x, f f) {f (Ref x); }
Void MakeDouble (Ref int x) {x = 2 * x; }
int i = 5;
Doaction (Ref i, MacDouble);

Note that Makedouble may be, unlike C++, the non-static method (!).

For normal cases, when your callback does not take Out / Ref arguments, there are harvested delegates, and almost always use them:

// FUNC & LT; INT, INT & GT; - function receiving int and returning too INT
int apply (int x, func & lt; int, int & gt; f) {return f (x); }
int getDouble (int x) {Return 2 * x; }
int i = 5;
i = apply (i, getduble);

or it is possible, with lambdam:

int i = 5;
i = apply (i, x = & gt; 2 * x);

Answer 2, Authority 40%

event?

I can be wrong, but I did something like this:

public class artdmxeventargs: Eventargs
{
  Public iPaddress Sender;
  Public Uint Universe;
  Public BYTE [] DATA;
}
Public Delegate Void ArtDmxhandler (Object Sender, ArtDMxEventargs E);
Class ArtNetnode.
{
  Public Event ArtDmxhandler OnartDMX;
  // ...
  Private Void FireOnartDMX (iPaddress Sender, Uint Universe, Byte [] Data)
  {
    if (onartdmx == NULL) Return;
    Artdmxeventargs e = new artdmxeventargs ();
    E.Sender = Sender;
    E.Data = DATA;
    E.UNIVERSE = Universe;
    onartdmx (this, e);
  }
  // ...
}

Client code:

{
  // Adding a handler
  Node.onartdmx + = new artdmxhandler (onartdmx);
}
Private Void Onartdmx (Object Sender, ArtDmxEventargs E)
{
  // Processor, called by the parish package
}

All callbacks, as far as I met, are made on such mechanics.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions