Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 851
Ezi Avatar asked Oct 02 '11 20:10

Ezi


People also ask

How do you convert F to C easily?

To convert temperatures in degrees Fahrenheit to Celsius, subtract 32 and multiply by . 5556 (or 5/9).

What is the formula to C?

The relationship between Fahrenheit and Celsius is expressed with the formula, °C = (°F - 32) × 5/9; where C represents the value in Celsius and F represents the value in Fahrenheit.

What is degrees C equal to in F?

Convert celsius to fahrenheit 1 Celsius is equal to 33.8 Fahrenheit.


2 Answers

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
like image 73
driis Avatar answered Oct 10 '22 11:10

driis


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
like image 40
Icarus Avatar answered Oct 10 '22 13:10

Icarus