Dave Noderer's Blog

August 2013 Entries

c# to vb.net Adding handlers with lambdas

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

APIMASH CNorris Windows 8 VB Starter Kit

A couple of months ago my esteemed Microsoft Developer Evangelist, Joe Healy visited our user group and spent some time going through the APIMASH starter projects the DPE’s have been working on. I was impressed!!

It looked like fun and thought that members of my user group would respond to these fairly simple but complete examples for Windows 8, Windows Phone 8 and now Windows 8.1.

Being a Visual Basic .net MVP I noticed that none of these were done with vb.net and know that there are millions of vb programmers out there looking for samples.. so I volunteered!

I started with Bob Familiar’s Chuck Norris c# starter kit, downloaded it from github and tried a few approaches to porting it.  You can read about Bob’s original starter kit here: http://theundocumentedapi.com/2013/06/06/apimash-chuck-norris-starter-kit/

There are two libraries, APIMASH_CNorrisVBLib and APIMASHVBLib plus the main windows 8 project.

Being the lazy developer, I tried to bulk convert c# to vb.net. Two I tried were http://www.developerfusion.com/tools/convert/csharp-to-vb/ and  http://converter.telerik.com/. Neither really did a complete job and both left some c# remnants and just plain errors. In the end I used the DeveloperConfusion throughout just so I would get use to what it was doing. In retrospect I should have keep more detailed notes and will next time. Three areas I remember having to rewrite the code was for adding event handlers, lamdas and overrides.

The two libraries were pretty straight forward and I was able to get them to compile and look correct pretty quickly. There were no tests and I did not tackle that task.

What took me longer was the actual windows 8 app. Trying to convert the code behind and past it into the xaml forms did not go well. In the end I created a fresh windows 8 project and spliced in and ported pieces. The main problem was that the namespaces got confused and mixed up so that the code behind was not recognizing the few controls on the page.

Once I was over that hump the project worked!

I’m sure that there are many improvements that could be made and hope to hear from you.

The project can be found on github under the Windows Starter Kits: https://github.com/apimash/StarterKits