Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
Saturday
Jul312010

Windows Phone taskhost.exe error

(Thanks to Diego H for his solution to this one. He posted the solution on the Windows Phone Jump Start forum and I’ve found it very useful.)

image

When I’m writing Windows Phone programs I’ve noticed that I sometimes get the above error when I try to deploy a Windows Phone 7 project to the emulator. This is usually after I have moved or copied the Visual Studio solution to another directory. 

If I don’t get this error I might notice an even stranger manifestation, where changes I’ve made to the program don’t seem to be reflected in the running code. It turns out that this is due to a build setting getting corrupted. To fix it you must go to the Build menu and select Configuration Manager. Then re-select Windows Phone as the Active solution platform, so that the dialog looks as shown below:

image

Then your programs will deploy and run OK. I’ve no idea why this happens, but sometimes this gets set to “Any Platform” with no build or deploy options set, which stops programs from being built or deployed.

Friday
Jul302010

Quick Number Formatting in C#

IMG_2184

I’ve been playing with the Windows Phone accelerometer, and displaying results. Thing is, it gives me values which have lots of decimal places that I don’t really want. They make the numbers flicker in a way that hurts my head. What I want is a way to just display them to two decimal places.

I must admit that previously I’ve resorted to very dodgy behaviour at this point, such as multiplying by 100, converting to an integer and then dividing by 100 again. Must be my old assembler ways coming back to haunt me. Turns out that there is a much easier way of doing this in C# which is to use the string formatter:

message = "Accelerometer" +
    "\nX: " + string.Format( "{0:0.00}",accelState.X) +
    "\nY: " + string.Format( "{0:0.00}", accelState.Y) +
    "\nZ: " + string.Format( "{0:0.00}", accelState.Z) ;

This little block of magic gets the X, Y and Z values out of a vector and then displays them to two decimal places in a string I can display in my XNA program.

The key is the {0:0.00} bit in the format, which gives a pattern for the display of the first (and only) value to be converted. This pattern says a single leading digit and two decimal places.

If you want to do interesting things like put commas into numbers (so that you can print 1,234,567 type values) then you can do that too. You can find out more about how the format strings work in my C# Yellow book (subtle plug) starting on page 50.

Thursday
Jul292010

Delegates, Events and Confusion

IMG_2283

Delegates and Events are really powerful parts of C#. They are also powerful confusing.  We can start off by making a new delegate type:

delegate void SimpleEvent () ;

This is a new type, called SimpleEvent. If we ever need to create a delegate that refers to a method that returns void and has no parameters, we can use SimpleEvent to do this. So, let’s do that. Here is our method:

void testMethod()
{
    Console.WriteLine("Test Called");
}

This method doesn’t do much, but it does tell us it has been called, which is a start. Now, to create something that refers to this method we can create a SimpleEvent instance:

SimpleEvent x = new SimpleEvent(testMethod);

This has created a delegate that refers to testMethod. We’ve given it the rather enigmatic name of x. We can make a call on this delegate if we want:

x();

Note that we have to treat x as if it was a void method that accepts no parameters (since that is the type of method that the delegate refers to). When x runs it will print out “Test Called”, since that is what testMethod does, and x presently refers to that method.

The name x is actually quite appropriate, in that when we call x we don’t really know what is going to happen, since it could be made to refer to any method. If it doesn’t refer anywhere we’ll get a null reference exception of course, and serve us right.

So, delegates let us create objects that can refer to methods we want to run. Great if you want to bind behaviours to events. (and this is where it starts to get confusing)

We can declare a delegate variable:

SimpleEvent eventList;

At the moment this refers nowhere (try to call eventList and we get an exception). But we can add methods that we want to be called when the event is fired:

eventList += new SimpleEvent(testMethod);

Now, when we do:

eventList();

- the testMethod is called. Wonderful. So let’s make this more confusing:

eventList += x;

This adds another item on to the list of delegates managed by eventList. The item added is the x that we created earlier, which refers to testMethod. Now, when we do:

eventList();

- the testMethod gets called twice (because when you call a delegate it works through all the methods that are presently bound to it).

If we want to unhook methods from a delegate then we can use –= to do this:

eventList –= x;

This would remove one of the calls of testMethod from the delegate, so that now if we call eventList it will only call testMethod once. And it works.

However, this works too:

eventList –= new SimpleEvent(testMethod);

I hate this. It confuses me so much. It looks like we’ve made a new delegate and minused (!) it from the list of events. There is some strong magic going on here which I really don’t like. What must happen is that the –= operator must go “Aha!, here is a delegate that references this method, I must remove one from the list of delegates”.

If x is the delegate that I originally created, it seems reasonable to remove that from the list, what confuses me is that you can also remove delegates by appearing to create new ones. I guess that this is because otherwise programmers would have to keep track of event references that they might like to remove later, but I still don’t like it.

Wednesday
Jul282010

New C# Yellow Book Now Available

C# Yellow Book 2010 Cover
Now with new “Crinkle Cut” cover

The new version of my famous C# Yellow Book is now available for download. This free 197 page book is the basis of our programming course and has a whole bunch of new content, along with reorganised layout and proper sections. Get yours today from here:

http://www.csharpcourse.com/

Tuesday
Jul272010

Back to Work

Cinema
Bellevue Movie Theatre looking good

..and today I go back to work…