1

I'm trying to convert some C# code to VB.Net. I think I'm dealing with lambda expressions here but I'm not sure how to convert this. I'm new to C# and this is a little bit more advanced than the free online converters can handle.

Converting the code to VB.net for me would be great but what I'm really looking for is someone to explain what this code is attempting to do (teach a guy to fish and all that...)

HubProxy.On<string, string>("AddMessage", (name, message) => 
                this.Invoke((Action)(() => 
                    RichTextBoxConsole.AppendText(String.Format    ("{0}: {1}" + Environment.NewLine, name, message)))));

BTW, the HubProxy object is a SignalR hub.

Thanks, kayaking_jeff 

0

2 Answers 2

1

I'm not particularly familiar with SignalR, but here's what it looks like is happening, working from the inside out

String.Format("{0}: {1}" + Environment.NewLine, name, message)

Substitutes variables into a string. name would take the place of {0} and message takes the place of {1}. This would be the same as new String(name + ": " + message + Environment.NewLine)

RichTextBoxConsole.AppendText(String.Format(...))

Appends the formatted string into a RichTextBox

(Action)(() => RichTextBoxConsole.AppendText(...))

Declares a lambda expression that takes no parameters and calls the AppendText function.

(name, message) => this.Invoke((Action)(...))

Declares a second lambda function, taking 2 parameters (name and message) and calling the Invoke function with the previous lambda expression. I presume Invoke will actually execute the lambda expression.

HubProxy.On<string, string>("AddMessage", (name, message) => ...);

Again, not familiar with SignalR, but my educated guess would be that this is attaching the previous lambda expression to an AddMessage event, such that whenever AddMessage occurs, your lambda function will execute, causing the name and message of the AddMessage event to be appended into the RichText box via the Invoke function (perhaps to ensure the UI element is modified on the appropriate thread?)

Anyway, hopefully this helps you parse the line. Can't help you with converting it though >.<

(If I have said something incorrectly, please let me know and I will remove or adjust the offending statements)

Sign up to request clarification or add additional context in comments.

Comments

0

The VB equivalent is:

HubProxy.On(Of String, String)("AddMessage", Function(name, message) _
    Me.Invoke(CType(Sub() _
    RichTextBoxConsole.AppendText(String.Format("{0}: {1}" & Environment.NewLine, name, message)), Action)))

See Tyler's answer for an explanation of the logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.