Converting C# samples to VB.NET
It seems that all of the good samples out there for Silverlight or any other .NET technology are written in C#. So f you’re a VB.NET developer, what are you to do?
There are some good tools out there to convert C# code to VB.NET, some are very good and cost a good deal of money. For a quick and dirty conversion of pieces of code, I have found the following to work pretty well.
You may have already used Red Gate’s Reflector application (formerly Lutz Roeder’s Reflector), downloadable for free here:
http://www.red-gate.com/products/reflector/
Reflector is typically used for digging into assemblies that you don’t have the source code for and figuring out how they work. However, by using the language dropdown, you can get a pretty decent language converter.
Let’s assume you have a sample written in C#. For this example, I’ll use a piece of code from our upcoming book, Hello! Silverlight 2. As a side note, you won’t have to use a method like this for converting the samples in our book since we will be including all of the samples as both C# and VB.NET inline in the book’s text.
If we select the dll output from a Silverlight project, and then select one of the classes in it, let’s say FishEyeMenuItem in our case, you’ll see the following:
Then you can click on the Expand Methods link in the right hand pane, and see all of the C# source code for this class:
Now if you look in the top toolbar, you’ll see a dropdown that says C#. Change that to “Visual Basic” and Expand Methods again and you’ll see something like this:
Now since this is code for a user control, there is generated code mixed in here, specifically the InitializeComponent method and some generated field definitions corresponding to XAML elements. So if you try to copy and paste this into a VB version of the project you would get duplicate definitions. It’s pretty easy to track those down and remove them though, and the code then should work as-is.
NewTeeVee Reports: Netflix to Lay Off 50. Blames Silverlight.
So I saw the headline and my heart sank. After news, some accurate and some distorted, showcasing wins of Flash over Silverlight comes this article:
http://newteevee.com/2008/12/08/netflix-to-lay-off-50-blames-silverlight/
The article refers to this announcement on the Netflix blog:
http://blog.netflix.com/2008/12/changes-in-customer-service.html
In the announcement, Steve Swasey from Netflix says the following:
For those of you who watch movies instantly on your PC or Mac, you may have noticed our player is much easier to install and use now with Silverlight. The good news is fewer problems for you. The bad news is that we are now overstaffed with technical specialists in our Customer Service (CS) group.
So last week we announced internally some changes in CS. 50 of our technical specialists will work through December, then be let go in early January after the holidays. 15 of our technical specialists will take new roles in the main CS group.
I hate to see more people getting laid off, but definitely some positive news for Silverlight.
Embedding a F# class library in a Silverlight 2 Application
I’ve been preparing for my F# session at the MSDN Developer Conference in Orlando on December 11 and came across a blog post on creating a Silverlight application using F#. Here is the original post:
http://jyliao.blogspot.com/2008/11/f-and-silverlight-20.html
Unfortunately it was only a description of how to do it, but there wasn’t a sample. In this post, F# was used to do everything. This isn’t really what I was looking for. I wanted to write a C# Silverlight application but call into an F# class library.
Disclaimer: This is a hack and I’m not responsible for what it might do to your system. Also I’m not sure of the licensing of the FSharp.Core.dll but I would guess that you can’t redistribute it, so only use it for testing until a true FSharp.Core.dll is available for Silverlight.
For those who aren’t familiar with F#, it’s a functional or declarative language that compiles into .NET byte code and is becoming popular for banking and scientific applications. You can read more about F# here:
http://msdn.microsoft.com/en-us/fsharp/default.aspx
Compiling F# for Silverlight is a bit tricky right now because a Silverlight version of the runtime hasn’t been released as far as I can tell, and there is no project wizard to create an F# Silverlight project.
After a lot of hacking, I’ve come up with something that will work, and is easy for anyone to start using. What I have created is a Visual Studio project template which creates a C# Silverlight application and also an F# class library.
FSharp.Core.dll is the F# runtime. Since it’s not compiled against the Silverlight runtime, can’t be added to a Silverlight project in Visual Studio, but if the project creation wizard puts it in there it seems to work ok.
The first thing you’ll need to do is install the F# 1.9.6.2 CTP
Then download this project template that I created:
http://www.bluerosegames.com/silverlightfsharpproject.zip
and put it in your Documents\Visual Studio 2008\Templates\ProjectTemplates folder. Once you do this, if you do a “New Project” in Visual Studio, you should see a new template under the “My Templates” section:
When you create this app, you’ll get some warnings about trusted imports, etc. If anyone can figure out how to get rid of these, please let me know.
What you’ll have after creating the project is a main Silverlight Application, a web application if you chose to create one, and a F# class library project. The F# class library would typically have a .fsproj extension, but I could only get it working if you use a .csproj extension, so there you go, told you it was a hack.
To get you started with a Hello World type sample app, the Module1.fs in the F# project is created with the following:
#light
namespace SLFSharpApp16_FSharp
type TestClass = class
new () as this = {}
member s.Hello() =
"Hello from F#"
end
This creates a class called TestClass with a method called Hello that returns a string.
Now in the main Silverlight project, the Page.xaml is generated with a TextBlock in it that we will populate with the text from the Hello method:
<UserControl x:Class="SLFSharpApp16.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="text" Text="Text from FSharp test will go here"/>
</Grid>
</UserControl>
and the Page.xaml.cs calls into the F# class library:
using SLFSharpApp16_FSharp;
namespace SLFSharpApp16
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
TestClass test = new TestClass();
text.Text = test.Hello();
}
}
}
If you run the solution without any changes after it’s created you should see the following, with the text coming from the F# Hello method:

Thoughts on the MIX 10K challenge
For those that haven’t heard, Microsoft has announced an interesting contest in conjunction with the MIX09 conference in Las Vegas. The way the challenge works is that you need to create a program where the source code is less than 10K bytes (10240 bytes) in Silverlight or WPF XBAP that will run on the web.
Rules and details are here:
http://2009.visitmix.com/MIXtify/TenKGallery.aspx
These types of competitions have been going on pretty much since computers have been around, and in particular, the Demoscene has some great samples of tiny programs doing amazing things.
The prizes are pretty sweet, including a trip to MIX09, $1500 Visa gift cards, and $500 Visa gift cards.
So a few thoughts on how to cram something into 10K.
First of all, forget a lot of what you learned about good programming techniques. Unless you’re ultimately saving space by breaking something into multiple methods or classes, shove everything together.
Make your variables and methods a single character if possible. You can write using longer names and refactor->rename later if it’s easier.
Reduce whitespace. C# can be written as a single line for the entire file.
Make sure to use using statements to avoid having to specify the namespace, but remove any unused using statements from your source files.
Use the built in .Net framework provided classes whenever possible instead of writing your own.
Consider using F# which can often use less characters for the same logic.
For any graphics, determine whether it’s more efficient to represent it as XAML or a bitmap image. Also consider writing code to create the graphics if it’s less characters than the XAML equivalent.
Anyone else have good suggestions to add to this?