tags:

views:

595

answers:

9

This is from an example accompanying the agsXMPP .Net assembly. I've read up on delegates, but am not sure how that fits in with this line of code (which waits for the logon to occur, and then sends a message. I guess what I'm looking for is an understanding of why delegate(0) accomplishes this, in the kind of simple terms I can understand. <s>

        xmpp.OnLogin += delegate(object o) { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); };

thanks,

Hank Fay

A: 

That is creating an anonymous function. This feature was introduced in C# 2.0

Abe Heidebrecht
A: 

It serves as an anonymous method, so you don't need to declare it somewhere else. It's very useful.

What it does in that case is to attach that method to the list of actions that are triggered because of the onLogin event.

André Neves
+3  A: 

It's exactly the same as

xmpp.OnLogin += EventHandler(MyMethod);

Where MyMethod is

public void MyMethod(object o) 
{ 
    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
}
Juan Manuel
Well, it's not EXACTLY the same, because within the anonymous method, you have access to the local variables of the scope where the delegate is defined.
Ishmaeel
You are right, though in this example it is
Juan Manuel
I think you would need a new before the EventHandler. And I believe that EVentHandler expects an EventArgs parameter. You would need to declare a new delegate type that only reguired an object parameter, I think anyway! See Remi's answer
Darrel Miller
It wasn't intended to be perfect in sintaxis, it was to ilustrate the concept
Juan Manuel
A: 

The "delegate(object o){..}" tells the compiler to package up whatever is inside the brackets as an object to be executed later, in this case when OnLogin is fired. Without the "delegate()" statement, the compiler would think you are tying to execute an action in the middle of an assignemnt statement and give you errors.

Gilligan
Amusing that out of all the excellent answers here, the most vague one gets picked.
FlySwat
A: 

Agreed with Abe, this is an anonymous method. An anonymous method is just that -- a method without a name, which can be supplied as a parameter argument.

Obviously the OnLogin object is an Event; using an += operator ensures that the method specified by the anonymous delegate above is executed whenever the OnLogin event is raised.

Jon Limjap
A: 

Basically, the code inside the {} will run when the "OnLogin" event of the xmpp event is fired. Based on the name, I'd guess that event fires at some point during the login process.

The syntax:

delegate(object o) { statements; }

is a called an anonymous method. The code in your question would be equivilent to this:

public class MyClass
{
  private XMPPObjectType xmpp;
  public void Main()
  {
    xmpp.OnLogin += MyMethod;
  }
  private void MyMethod(object o)
  {
    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?"));
  }
}
Jonathan
A: 

You are subscribing to the OnLogin event in xmpp.

This means that when xmpp fires this event, the code inside the anonymous delegate will fire. Its an elegant way to have callbacks.

In Xmpp, something like this is going on:

   // Check to see if we should fire the login event
   // ALso check to see if anything is subscribed to OnLogin 
   // (It will be null otherwise)
   if (loggedIn && OnLogin != null)
   {
       // Anyone subscribed will now receive the event.
       OnLogin(this);
   }
FlySwat
+2  A: 

As Abe noted, this code is creating an anonymous function. This:


xmpp.OnLogin += delegate(object o) 
   { 
      xmpp.Send(
         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
   };

would have been accomplished as follows in older versions of .Net (I've excluded class declarations and such, and just kept the essential elements):


delegate void OnLoginEventHandler(object o);

public void MyLoginEventHandler(object o)
{
      xmpp.Send(
         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
}

[...]

xmpp.OnLogin += new OnLoginEventHandler(MyLoginEventHandler);

What you're doing in either case is associating a method of yours to run when the xmpp OnLogin event is fired.

Remi Despres-Smyth
+1  A: 

OnLogin on xmpp is probably an event declared like this :

public event LoginEventHandler OnLogin;

where LoginEventHandler is as delegate type probably declared as :

public delegate void LoginEventHandler(Object o);

That means that in order to subscribe to the event, you need to provide a method (or an anonymous method / lambda expression) which math the LoginEventHandler delegate signature.

In your example, you pass an anonymous method using the delegate keyword :

xmpp.OnLogin += delegate(object o)
                { 
                    xmpp.Send(new Message(new Jid(JID_RECEIVER), 
                              MessageType.chat,
                              "Hello, how are you?")); 
                };

The anonymous method match the delegate signature expected by the OnLogin event (void return type + one object argument). You could also remove the object o parameter leveraging the contravariance, since it is not used inside the anonymous method body.

xmpp.OnLogin += delegate
                { 
                    xmpp.Send(new Message(new Jid(JID_RECEIVER), 
                              MessageType.chat,
                              "Hello, how are you?")); 
                };
Romain Verdier