Geeks With Blogs

News

Dave Noderer's Blog South Florida does .NET!

I am converting some of the APIMASH projects from c# to vb.net and having to translate between the two.

You can find the APIMASH projects at: https://github.com/apimash/StarterKits

This is part of inheriting behavior of the System.Net.WebClient, for Windows Phone 8.

The c# code looks like this:

DownloadStringCompleted += (s, e) =>
{
    if (e.Error == null)
    {
        tcs.TrySetResult(e.Result);
    }
    else
    {
        tcs.TrySetException(e.Error);
    }
};

I tried two c# to vb.net conversion tools, DevFusion: http://www.developerfusion.com/tools/convert/csharp-to-vb/ and Telerik thttp://converter.telerik.com/.

Simple code converts fine but this example that does not, both do the same thing, could be the same engine:

DownloadStringCompleted += Function(s, e)
                               If e.[Error] Is Nothing Then
                                   tcs.TrySetResult(e.Result)
                               Else
                                   tcs.TrySetException(e.[Error])
                               End If

                           End Function

The lambda function works ok but that is not how event handlers are written in vb.net, this works much better:

AddHandler DownloadStringCompleted, Function(s, e)
                                        If e.[Error] Is Nothing Then
                                            tcs.TrySetResult(e.Result)
                                        Else
                                            tcs.TrySetException(e.[Error])
                                        End If
                                    End Function

Posted on Sunday, August 25, 2013 7:59 PM | Back to top


Comments on this post: c# to vb.net Adding handlers with lambdas

# c# to vb.net Adding handlers with lambdas
Requesting Gravatar...
Really helps me to update my knowledge in Adding handlers with lambdas
Left by sriram on Jan 06, 2014 7:23 AM

# re: c# to vb.net Adding handlers with lambdas
Requesting Gravatar...
helping to finish my project earlier
Left by archana on Jan 06, 2014 7:29 AM

Your comment:
 (will show your gravatar)


Copyright © Dave Noderer | Powered by: GeeksWithBlogs.net | Join free