简体   繁体   中英

How would I convert C# delegate function to VB.Net?

Here there's an old question about this code.

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

I want to use it in vb.net (version 10) but I couldn't figure out how to convert it.

The delegate is an anonymous function. The syntax is a bit different for VB .NET, as expected. Without having the VB compiler at hand, I would say you need something like:

AddHandler xmpp.OnLogin,
    Sub(o As Object)
        xmpp.Send(
                    new Message(
                        new Jid(JID_RECEIVER),
                        MessageType.chat, 
                        "Hello, how are you?"
                    )
    End Sub

I don't know how to declare an anonymous delegate in VB.NET and I'm too lazy to Google it, but something like this should work (warning: not tested):

AddHandler xmpp.OnLogin, AddressOf Me.HandleSendMessage

Private Sub HandleSendMessage(ByVal o As Object)
xmpp.Send( new Message(
               new Jid(JID_RECEIVER),
                            MessageType.chat, 
                            "Hello, how are you?"
                        )
                    )
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM