开发者

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

开发者 https://www.devze.com 2023-04-09 20:03 出处:网络
Herethere\'s an old question about this code. xmpp.OnLogin += delegate(object o) { xmpp.Send( new Message(

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号