Silverlight Starter Tips

I ran into two problems yesterday I thought I share it with you all and document it for future reference. I am using Silverlight 4 and VS2010.

1. I was writing a Silverlight Unit Test. When I compiled my simple test, it keep failing saying, Unable to load ‘System.Windows’ or its dependencies. It turned out, we need to make the System.Windows property to copy always.

2. I was debugging my Silverlight code and was able to step into my client code just fine. After some time, my debugging stopped working, none of my break points were hit. I was using Chrome as my default browser. I changed the browser to IE and debugging start working. So if you are a Silverlight developer do your self a favor and use IE other wise you will end up spending some time trying to figure this debugging problem.

Advertisements

My First look at Kinect SDK

It is not really first look, there have been people hacking Kinect the day, it was launched. If you have not seen the hacks, go to You tube and search Kinect hacks. They bunch of videos on it. Last weekend I attended Kinect for Developers @ Plano, TX. Shawn and gang did excellent job in organizing the event. The speakers were good. The best part was lab. Anyway, if any of you want to learn, head to Microsoft page to download. Hold on the thought on downloading, If you are coming in for the first time, I would recommend wait till February 1st to download the SDK. On Feb 1st, Microsoft is releasing a new version of Kinect and new SDK to go with it. So I would strongly recommend wait and download and start cooking.

I was really impressed with what I have seen and what I was able to accomplish in two days time. In my opinion, if you know XNA and if you are a game developer, you have a great head start since it is easy to add the gestures logic to the code and get input from the there and your code stays the same. If you are XNA developer make sure you go to app hub link.Also if you are a WPF/Silverlight Developer you also have head start. The interesting thing is, Kinect provides another medium of input and there are libraries available that you can use or extend to solve most of the common problems.

I have not decided to buy the Kinect that will be coming out on February 1st yet. I was told there will face reorganization and identify the figures. I think that will change the way we play games.

Passing TYPE around in C# (Generics)

Yesterday when I was doing a code review for one of my co-worker, I saw a scenario where there were two methods, where both the method does the same thing except for each one different types. It is a silverlight code, where he need to find a grid and another child element. By looking at the code, it begs refactoring, he was able to refactor the code easily with generics. When we did the refactor, I was wondering if there is another way of doing it without generics and it is. So here are the two ways to pass the types around and perform your check in a common function.

For our discussion we are going to have a heterogeneous collection, where we will an element of String, int, float and a List of int, like the following

genericList.Add("Unni");
genericList.Add(10);
genericList.Add(99.99);
List<int> tempList = new List<int>() { 2, 3 };
genericList.Add(tempList);

next we are going to write a method which will accept a TYPE as input and check if the type exists in the collection. The first and simple approach is by  passing the type itself as a parameter as shown below

public object CheckType(Type type)
{
    foreach (var t in genericList)
    {
        if (type.IsInstanceOfType(t))
            return t;
    }
    return -99;
}

Here is the associated test for his method

[Test]
public void It_Can_Return_10_When_int()
{
    Class1 c1 = new Class1();
    Assert.AreEqual(10, c1.CheckType(typeof(int)));
}

We walk though the collection and check each element is instance of a required type we need to find. If we find it, it will return the element from the collection. Now lets look at the second option using generics as it is supposed to be written

public object CheckType<T>()
{
    foreach (var t in genericList)
    {
        if (t is T)
            return t;
    }
    return -99;
}

and associated Tests

[Test]
public void It_Can_Return_10_When_int_As_T()
{
    Class1 c1 = new Class1();
    Assert.AreEqual("Unni", c1.CheckType<string>());
}

[Test]
public void It_Can_Return_List_When_List()
{
    Class1 c1 = new Class1();
    List<int> tempList = new List<int>() { 2, 3 };
    Assert.AreEqual(tempList, c1.CheckType<List<int>>());
}

We create a method and pass generic type T. Now the code which this is very elegant because you do not pass the type as the input parameter.

Collection Assertion in MSTest and Kata

I have been doing Prime Factor Kata with one of my friend. If you want to follow along, there is a great video post at Rickard Nilsson blog. For this Kata, my first attempt was to use C# (obviously), NUnit. All worked very well. So out of curiosity, I decided to change it a little bit, use MSTest. I was expecting the same code I wrote in NUnit would work in MSTest but to my surprise, even my first test failed. Here is my first test

Assert.AreEqual(1.Primes(), new List<int>());

Here is the Primes implementation

public static List<int> Primes(this int n)
{
   return new List<int>();
}

Very simple. In NUnit, when you assert a condition on a collection, it just does assertion, while in MSTest, you can not use Assert against a collection.If you need to do assert against a collection, you need to use CollectionAssert.AreEqual. So when I changed the code to CollectionAssert, it worked like a charm. But here is a catch though, if I had the code like

public static IList<int> Primes(this int n)

The assertion will not work since IList does not implement ICollection, rather it implements ICollection<T>. There are ways to work around that, but I found a better way of doing the whole thing anyway. If you want to do it like NUnit, then, what I would recommend is to install NBehave. Specifically the NBehave for .Net. With that installed, now the same test I used in the beginning of the blog would be like this and more readable.

1.Primes().ShouldBeEqualTo(new List<int>());

Which is more readable and the same code works with NUnit and MSTest. The whole benefit of Kata for me besides the obvious, I was able to compare little bit of MSTest and NUnit. Based on what I have done, I decided to use NUnit just for ‘Parameterized Test Fixtures’. One of the thing we do while doing the Kata is code, refactor. It turned out, all my tests were copy paste except the input and expected result.So rather than have eight separate tests now I have one consolidated test with the input and expected result been passed in as attribute to NUnit. Here is the final code

[TestCase(1, new int[0])]
[TestCase(2, new int[1] {2})]
[TestCase(3, new int[1] {3})]
[TestCase(4, new int[2] { 2, 2 })]
[TestCase(6, new int[2] { 2, 3 })]
[TestCase(8, new int[3] { 2, 2, 2 })]
[TestCase(9, new int[2] { 3, 3 })]
[TestCase(3*5*7*11, new int[4] {3,5,7,11})]
public void Should_Find_Primes(int t1, int[] t2)
{            
     Assert.AreEqual(t1.Primes(), t2);
}

Thats all about it, good way to start a new year by learning something new and cool.

Happy 2012

First of all, ‘Wish you all very happy 2012’. I had a lot of fun the last two weeks, playing games #swtor also did some windows phone development. I am working on a windows phone game but the problem is that I do not have a windows phone to do testing. I need to find a way to get a loner windows phone so that I can test my game.

It was refreshing to read, Jeremy’s blog on Silverlight future. Regardless, I started working on various small HTML5/jQuery projects to make my self aware of what is happening at that end. I keep posting my finding as well.

This is I plan to write more blogs on Windows Phone and start look at Windows 8 and WinRT.