Laserbrain — a software blog

From the heart and brain of a C# developer

Best practices of the Dictionary class

clock February 9, 2014 15:50

It’s important to know how the standard classes behave so that your code gets optimized, not performing unnecessary checks or making unnecessary calls.

Here's a number of issues I've seen time after time:

Fetching a value from the dictionary only if it exists

In the first version (bad) there are two dictionary lookups performed. It’s like walk in to a public library asking the librarian whether a certain book is in – and when the librarian comes back and says “yes, it’s in”, you ask again saying "Good, could you please get it for me?". In real life it's pretty obvious that this is a silly behavior, but in the computer world it's not that apparent.
if (myDict.ContainsKey(key)) // First lookup.
	ValueType value = myDict[key]; // Second lookup.
	// ... use the "value" here ...
In the second version (good), only one lookup is performed:
ValueType value;
if (myDict.TryGetValue(key, out value))
	// ... use the "value" here ...

Accessing all the keys and values

I've seen many real-life cases where access to both the key and value is needed in a foreach-loop, and the iteration is performed over the Keys-properties while the value is accessed by a lookup:
foreach (TKey key in myDict.Keys)
	TValue value = myDict[key];
	// ... use the "key" and "value" here ...
If you want both the key and the value simultaneously it’s better to iterate over the dictionary as KeyValuePairs:
foreach (KeyValuePair<TKey, TValue> keyValuePair in myDict)
	TKey key = keyValuePair.Key;
	TValue value = keyValuePair.Value;
	// ... use the "key" and "value" here ...
(Obviously; if you want only the keys you should use the Keys-property, and if you want only the values use the Values-property)

Overwriting a value

if (myDict.ContainsKey(key)) // First lookup.
	myDict.Remove(key); // Second lookup.
myDict.Add(key, value); // Third lookup.
There is really no need to remove a key before assigning a new value to it:
myDict[key] = value;

Removing a value

Once again, the following implementation leads to two lookups:
bool entryExists = myDict.ContainsKey(key); // First lookup.
if (entryExists)
	myDict.Remove(key); // Second lookup.
return entryExists;
The Remove-method is actually very kind. It doesn't crash if you try to remove something that is not there but returns a bool telling the result of the operation:
return myDict.Remove(key);

When may my call fail?

The call... ...fails when
myDict[key] = value;
Never (*)
The value will be added if it's not already in the dictionary, and it will be replaced if it does.
var value = myDict[key];
When the entry “key” does not exist.
myDict.Add(key, value);
When the entry “key” already exists.
myDict.TryGetValue(key, out value);
Never (*)
Never (*)
(And the call returns false if the entry “key” does not exist.)
(*) Well, you get an ArgumentNullException if the key is null...

Fixing the assembly hopping between WP7 and WP8

clock April 28, 2013 16:47

If you want to develop an Windows Phone app that can run on both WP7 and WP8 you can simply submit one built for Windows Phone 7. But if you want to take advantage of some of the new feature of Windows Phone 8 you will need to submit two versions of it (one for 7 and one for 8). This means that you will have to maintain two copies of the project file -- but you obviously don't want two copies of each source code file.

This can be solved by putting the code in a Portable Class Library and then reference it by the two projects. There are however some limitations using a Portable Class Library, and you might find yourself stuck between a rock and a hard [coded] place trying to use it.

Each time I was facing the dual project problem, I ended up with using linked source code files instead, i.e. making one of the projects the "main" project, and then simply linking all the source code files into the other project (i.e. "Add As Link" rather than "Add" when adding "Existing Item..."). An annoying problem with this solution is however that each time you rename a source file you will need to remove and add the linked file again.  Aaarrgghh!  :[  But for me this actually was less painful than using the Portable Class Library.

Anyway, this way you will only have to maintain one copy of the code files that are identical on both platforms. For the ones that differs a bit, you either use compiler switches (e.g. "#if WP7 [...] #endif") -- or totally different classes (i.e. non-linked code, if there are "too much" differences to make use of #if's).

But when it comes to XAML you are in more trouble. For instance, if you are sharing the same implementation of a page in both WP7 and WP8, you will notice that the home of the Pivot (and Panorama) has changed between the two platforms. Ouch.

In WP7 your page declaration looks like this:


but your WP8 projects want this line:


As you can see above, the assembly name has changed.

This makes you yearn for the following (illegal) construction:

#if WP7
#elif WP8

But unfortunately compiler switches are not allowed in XAML... So the above will NOT compile. :(


There is however a pretty simple solution to this. Create the following two classes:

namespace MyShared
	using Microsoft.Phone.Controls;

	public class MyPivot : Pivot

	public class MyPivotItem : PivotItem

This file can be linked across the two platforms intact without any need of #if's (since it wasn't the namespace that was changed, "just" the assembly).

Now you simply use the MyPivot wrapper classes (instead of the Pivot directly) in your XAML, and you're good to go:


        <MyShared:MyPivot Title="MY APPLICATION">
            <MyShared:MyPivotItem Header="item1">
            <MyShared:MyPivotItem Header="item2">


Running a "Hello World" written in C# on a Raspberry Pi for Linux n00bs like me

clock November 21, 2012 20:57

A while ago I found myself in the possession of a Raspberry Pi.

O the joy of a new toy! But what to do with it?

Well, being a C# developer of course I would like to be able to install and run my own applications on it — and I wanted to do that remotely from my regular laptop.

The biggest problem was that I have no Linux skills what so ever. I've been living a comfortable (well, hm... you know...) Windows life ever since the days of the Amiga and Commodore 64. But how hard could it be?

The following are the steps I had to go through to make it work. It's a hodgepodge of wisdoms found all over the internet. So; disclaimer: It worked for me, but I can't promise it will work for you!


Here we go...


Step 1. Formatting the SD card

To do this properly you'll need to use the SD Formatter 3.1 application.

Install it and launch it having your SD card inserted. Simply press the Format button. It's enough to just do a quick format.

Important note!

If you are repeating this step later on, your SD card might appear to have lost a few gigabytes, only showing something like 56MB! This is due to that Windows cannot see the Linux partitions, and the 56MB is just some left-overs that Linux didn't claim.

You have at least a couple of options to reclaim the full space in Windows:

  1. Find yourself a [video]camera or any other non-windows device and try to format it in there. I first tried my age-old compact camera, but it just said that the card was bad. Then I tried my somewhat newer video camera, and it formatted it nicely and I got back my gigabytes. Phew...
  2. Use the dreaded (?) Flashnul (blog post) tool. I haven't tried this one since my first option worked for me. Be careful...


Step 2. Installing a Linux distribution on the freshly formatted SD card

I used the Raspbian “wheezy” image. (I was somehow drawn to its tag-line "If you’re just starting out, this is the image we recommend you use".)

To write the image to the card you'll need the Image Writer for Windows application. There is no installation required; just download and launch the Win32DiskImager.exe. Launch it, click the blue little folder-button and point out the image file. Then press the Write button to start writing. This will take a few moments, depending on the size and speed of your SD card.


Step 3. First launch

After preparing the SD-card it's time for the first launch.

Insert the SD card into the Pi and plug in an ethernet cable (with internet access), a USB keyboard and a monitor/tv (hdmi). Finally plug in the micro USB power adapter to boot it up. You will see a whole lot of text burst out on the screen. You do not need to read it all. :)

After a while a classic ASCII-artish gray popup appears, entitled Raspi-config.

Here you can for instance set your keyboard layout. When you're done, use the tab key to move to the Finish button, then press enter to access the console.

This popup is shown only the very first launch time. The following times you start the Pi you will need to enter a user name and password to access the console. The default user name is "pi" and the password is "raspberry".


Step 4. Optional: Setting the keyboard locale

If you didn't change the keyboard layout in that first popup (which only will show up upon the first launch), you can do it using the following command:

sudo nano /etc/default/keyboard

Where 'sudo' means 'run this command as super user', 'nano' is a text editor and '/etc/default/keyboard' is the file to edit.

Now the text editor appears, and you may change the 'XKBLAYOUT' to whatever you like. I chose 'se' for Sweden.

Save with Ctrl + O (!) and exit with Ctrl + X.

Reboot. This can be done by pulling the power cord or by issuing the command

sudo reboot


Step 5. Set a fixed IP-address

Since you want to remotely connect to your Raspberry Pi you might want to give it a fixed IP-address. This step is of course optional.

If you want to know the current network settings on your Pi, execute the following command:


(No, that is not a typo. It should be an 'f', not a 'p'...) You will see the current IP-address under the section 'eth0', on the line starting with 'inet'. (The MAC address is found after the word 'HWaddr'.)

Edit the network settings by executing:

sudo nano /etc/network/interfaces

When the editor appears, replace the line

iface eth0 inet dhcp

with these lines (the #-mark means that the line is commented out.)

#iface eth0 inet dhcp
iface eth0 inet static

Obviously you will need to fill in your preferred numbers here...

After saving the file you'll need to reboot. The best thing is of course to check your new settings by executing the ifconfig-command again, after rebooting.


Step 6. Ensure the SSH service is started at boot time

This is necessary for connecting remotely. It was however already active on the image I used, but I'll keep this step here anyway.

The command to execute is

sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc

which means to rename/move the file/directory specified. I got the following error message:

mv: cannot stat `/boot/boot_enable_ssh.rc': No such file or directory

which I interpreted that the SSH service was already active for me.


Step 7. Connect remotely from your favorite Windows PC

Download PuTTY.

There's no installation here, just an exe-file. Launch it and fill in the IP-address to your Pi (and the port, which defaults to 22) and connect with the Open button.

You will get a warning of a potential security breach the first time you connect, because your PuTTY instance (obviously) does not recognize your Pi.

I guess it is quite harmless to press the Yes button here.

Now the Pi console window greets you by asking for your username (pi) and password (raspberry). You're in!


Step 8. FTP

In addition to execute command on your Pi you will also need to transfer files to and from it. Use your favorite FTP client, for instance FileZilla. Simply connect to the correct IP address, port 22, user "pi" and password "raspberry".


Step 9. Install the C# compiler and .net exe-executer

To be able to compile and execute C#-code on the Raspberry Pi, you will need to install Mono.

This turns out to be pretty simple. Just execute the following command to install the dmcs (4.0 mscorlib) C# compiler:

sudo apt-get install mono-dmcs

This will take a while to process, the files requested will be downloaded from the internet and installed by some magic Linux fairy. Please note that in the beginning you will get a "Do you want to continue"-question. After saying Y + enter you might want to get a quick cup of coffee (or a beer since this is most probably done off-business hours). It will take a couple of minutes.


Step 10. Compile and run your program.

Now, I wanted the full monty and even compile the C# code on the raspberry. So, in order to make it as simple as possible, I put all the code into one file (HelloPi.cs):

namespace Test
	using System;

	public class HelloPi
		public static void Main(string[] args)
			Console.WriteLine("Hello World!");


Using the FTP client, transfer your 'HelloPi.cs' source file to a suitable folder. Compile the source file with the dmcs-command:

dmcs HelloPi.cs

Since it's not a very large application, it will be compiled pretty much instantly. Launch it with the mono-command:

mono MyApp.exe

Joy to the world, my app has launched! Laughing

Important note!

You might as well do the compilation on your PC (which is Much. More. Convenient.) and just transfer the exe-file to the Pi instead.


Some useful links


Good luck! Wink


Announcing the Diversify app for Øredev 2012

clock October 31, 2012 21:02

A collegue of mine, Fredrik Mörk, noticed the announcement of the Øredev 2012 mobile app contest, and soon a team was formed that happily started hacking away (hosting the code in a Mercurial repository on BitBucket, and also using Trello to have some sort of control of who was what, and what needed to be done). The team consisted of Micael Carlstedt, Niclas Carlstedt, Fredrik Mörk, Markus Wallén, Sebastian Johansson and myself.

The app is supposed to function as a companion before and during the conference. The main functionality is that it offers you to easily navigate and explore in the conference program, and build your own personalized conference program by “favoriting” sessions. It also features a twitter feed, listening to stuff related to the conference and the app. One focus of the design has been to allow you to explore. From almost every view, there is a way to move on and get information about related stuff. One example is when you are looking at a session, from this view you can in one touch navigate to

  • a page highlighting the room in which the session is on a map
  • a page showing all sessions in that room during the conference
  • a page showing details about the speaker (OK, this typically requires two interactions; scroll to the bottom of the page and then tap the speaker)
  • a page showing details about each other session running in other rooms at the same time
  • a page showing all sessions for a topic that this session also has

This may be the most extreme example, but it’s not unique in its concept.

One other thing that the app does is that it invites to sharing information about the conference. You can (again with very few interactions) share info about a session or speaker to social networks.

There is an update submitted to the market place which will add a couple of features (tell the app what days you will attend and it will filter the data throughout the app accordingly and, as a personal touch, the app authors’ suggestions for some sessions we find extra interesting). If you happen to have an unlocked developer phone, you can grab the XAP file for that update here. Otherwise, go ahead and get the app from the Windows Phone Marketplace.


(Thank you Fredrik, for allowing me to steal your words from your blog postSmile)


Some screen shots (click for full size):
Splash screen My Øredev List of sessions Session details

Speaker details Twitter feed Tweet detail About us

Rendering the audio captured by a Windows Phone device

clock March 11, 2012 17:11

This small app displays the audio captured by the microphone of a Windows Phone device and displays it as a continous waveform on the screen using the XNA framework.

A slightly modified version of the app (changing color when touching the screen) can be found in the Windows Phone Marketplace.


In order to capture the audio on a Windows Phone device, you need an instance to the default microphone (Microphone.Default), decide how often you want samples using the BufferDuration-property and hook up the BufferReady-event. Then you control the capturing with the Start() and Stop() methods.

The microphone is giving you samples at a fixed rate of 16 000 Hz, i.e. 16 000 samples per second. There is a property SampleRate that will tell this value. This means that you won't be able to capture audio of higher frequency than 8000 Hz (without distortion) according to the sampling theorem.

You are also limited when it comes to choose the value for the BufferDuration-property; it must be between 0.1 and 1 seconds (100 - 1000 ms) in 10ms-steps. This means that you must choose a value of 100, 110, 120, ..., 990, 1000 milliseconds.

When the microphone event BufferReady is fired, you should call the microphone.GetData(myBuffer)-method, in order to copy the samples from the microphone's internal buffer to a buffer that belongs to you. The recorded audio comes in the form of a byte-array, but since the samples are actually signed 16-bits integers (i.e. an integer in the range of -32'768 ... 32'767), you will probably need to do some convertion before you can process them.

How this application works

The way this application works is keeping a fixed number of narrow images, here called "(image) slices", arranged in a linked list. The images are rendered on the screen and smoothly moved from the right to the left. When the left-most slice has gone off the screen, it is moved to the far right (still outside the screen) in order to create the illusion of an unlimited number of images.

Each slice holds the rendered samples from the content of one microphone buffer. When the buffer is filled by the microphone mechanism, the rightmost slice (outside of the screen) is rendered with these new samples and started to be moved inwards the screen.

The speed of how fast the slices are moving across the screen is correlated to the duration of the buffer in such a way that the slices are moved a total of "one slice width" during the time the microphone is capturing the next buffer.

Since the buffer of captured audio is rendered as graphic on a texture as soon it is received, there is no reason to keep any old buffer data. Therefore the application only keeps one buffer in memory which is reused over and over.

A flag is set each time the microphone buffer is ready. Since the BufferReady event is fired on the main thread, there is no need for any lock-mechanism.

In the Update()-method of the XNA app, the flag is checked whether new data has arrived, and if so, the slice in line is drawn. In the Draw()-method, the slices are drawn on the screen and slightly moved as time goes by.

The complete Visual Studio solution file can be downloaded from here.

Here's a description of the structure of the main "Game"-class.

Some constants:
// The size of the screen.
private const int LandscapeWidth = 800;
private const int LandscapeHeight = 480;
// The number of milliseconds per time slice.
private const int SliceMilliseconds = 100;
Fields regarding the microphone and the captured data: 
// The microphone and the sample data.
private readonly Microphone microphone;
private readonly byte[] microphoneData;

// The time it takes for a sample to pass over the screen.
private readonly TimeSpan screenMilliseconds = TimeSpan.FromSeconds(5);
Choose a color that is almost transparent (the last of the four parameters; it's the red, green, blue and alpha-component of the color). The reason is that many samples are drawn on top of each other, and keeping each individual sample almost see-through makes an interesting visual effect. 
// The color of the samples.
private readonly Color sampleColor = new Color(0.4f, 0.9f, 0.2f, 0.07f);
The drawing classes. The white pixel texture is doing all the drawing. 
// The sprite batch and a white single-pixel texture for drawing.
private SpriteBatch spriteBatch;
private Texture2D whitePixelTexture;
The size of each image slice. 
// The size in pixels of one time slice.
private int imageSliceWidth;
private int imageSliceHeight;
There's no need to keep a reference to the linked list itself; just the first and last link. These links keeps references to their neighbors. The currentImageSlice is the one to draw on the next time. 
// The first, last and current image slices.
private LinkedListNode<RenderTarget2D> firstImageSlice;
private LinkedListNode<RenderTarget2D> lastImageSlice;
private LinkedListNode<RenderTarget2D> currentImageSlice;
The speed of the slices moving across the screen. 
// The current speed of the samples.
private float pixelsPerSeconds;
In order to know how far the current samples should be moved, the application must keep track of when they appeared. 
// The time in seconds when the current microphone data appeared.
private float microphoneDataAppearedAtSeconds;
 The signal that tells the Update()-method that there is new data to handle. 
// A flag telling whether new data has been read.
private bool hasNewMicrophoneData;
 The density of samples per pixel. 
// The number of samples squeezed into the width of one pixel.
private int samplesPerPixel;
Here's the constructor. In it the graphics mode is set and the microphone is wired up and asked to start listening. 
public Waveform()
    // Set the screen mode.
    new GraphicsDeviceManager(this)
        PreferredBackBufferWidth = LandscapeWidth,
        PreferredBackBufferHeight = LandscapeHeight,
        IsFullScreen = true,
        SupportedOrientations =
            DisplayOrientation.Portrait |
            DisplayOrientation.LandscapeLeft |

    // Standard setup.
    Content.RootDirectory = "Content";
    TargetElapsedTime = TimeSpan.FromTicks(333333);
    InactiveSleepTime = TimeSpan.FromSeconds(1);

    // Refer to the default microphone and hook the BufferReady-event.
    microphone = Microphone.Default;
    microphone.BufferReady += MicrophoneBufferReady;

    // Set the buffer duration to the length of one slice.
    microphone.BufferDuration = TimeSpan.FromMilliseconds(SliceMilliseconds);

    // Calculate the size in bytes of the sound buffer and create the byte array.
    var microphoneDataLength = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
    microphoneData = new byte[microphoneDataLength];

    // Start listening.
In the XNA's LoadContent nothing is actually loaded since the app is not dependent on any predrawn images. The SpriteBatch is created, the white pixel texture is generated and the image slices are initialized (as black images). 
protected override void LoadContent()
    // Create a SpriteBatch for drawing.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    // Create a 1x1 texture containing a white pixel.
    whitePixelTexture = new Texture2D(GraphicsDevice, 1, 1);
    var white = new[] { Color.White };

    // Create the image slices.
The CreateSliceImages is calculating how many slices that are needed to cover the entire screen (plus two so there's room for movement). In the end of the method the regular RenderSamples-method is called in order to initial all the images. Since there is no data yet (all samples are zero) it will generate black images. 
private void CreateSliceImages()
    // Calculate how many slices that fits the screen (rounding upwards).
    var imageSlicesOnScreenCount = (int)Math.Ceiling(screenMilliseconds.TotalMilliseconds / SliceMilliseconds);

    // Calculate the width of each slice.
    imageSliceWidth = (int)Math.Ceiling((float)LandscapeWidth / imageSlicesOnScreenCount);

    // Set the height of each slice to the largest screen size
    // (this way the full height is utilized in Portrait mode without stretching)
    imageSliceHeight = LandscapeWidth;

    // Create a linked list with the required number of slices, plus two
    // so that there's room for scrolling off-screen a bit.
    var imageSlices = new LinkedList<RenderTarget2D>();
    for (var i = 0; i < imageSlicesOnScreenCount + 2; i++)
        var imageSlice = new RenderTarget2D(GraphicsDevice, imageSliceWidth, imageSliceHeight);

    // Reference the first, last and current slice.
    firstImageSlice = imageSlices.First;
    lastImageSlice = imageSlices.Last;
    currentImageSlice = imageSlices.Last;

    // Calculate the speed of the pixels for an image slice.
    pixelsPerSeconds = imageSliceWidth / (SliceMilliseconds / 1000f);

    // Since the byte-array buffer really holds 16-bit samples, the actual
    // number of samples in one buffer is the number of bytes divided by two.
    var sampleCount = microphoneData.Length / 2;

    // Calculate how many samples that should be squeezed in per pixel (width).
    samplesPerPixel = (int)Math.Ceiling((float)sampleCount / imageSliceWidth);

    // Iterate through all the image slices and render with the empty microphone buffer.
    var slice = firstImageSlice;
    while (slice != null)
        slice = slice.Next;
The XNA's UnloadContent is just cleaning up what the LoadContent created. 
protected override void UnloadContent()
    // Dispose the SpriteBatch.

    // Dispose the white pixel.

    // Dispose all the image slices.
    var slice = firstImageSlice;
    while (slice != null)
        slice = slice.Next;
The event handler to the microphone's BufferReady-event. It copies the data from the microphone buffer and raises the flag that new data has arrived. 
private void MicrophoneBufferReady(object sender, EventArgs e)
    // New microphone data can now be fetched from its buffer.

    // Copy the samples from the microphone buffer to our buffer.

    // Raise the flag that new data has come.
    hasNewMicrophoneData = true;
The XNA's Update method checks the phone's Back-button to see if it's time to quit. After that it checks the flag to see if new data has been recorded. If so, the new samples are rendered by calling the RenderSamles-method. 
 protected override void Update(GameTime gameTime)
    // Exit the app if the user presses the back-button.
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    // If new data has been captured, a new slice should be drawn.
    if (hasNewMicrophoneData)
        // Reset the flag.
        hasNewMicrophoneData = false;

        // Express the current point in time as "seconds passed since start of app".
        var currentSeconds = (float)gameTime.TotalGameTime.TotalSeconds;

        // Remember the current time as "when the mic data appeared".
        microphoneDataAppearedAtSeconds = currentSeconds;

        // Render the new samples on the current image slice.

        // Select the next image slice as the new "current".
        currentImageSlice = currentImageSlice.Next ?? firstImageSlice;


The XNA's Draw-method takes care of drawing the rendered slices. It handles the two screen orientation modes; landscape and portrait, by scaling the images accordingly. If it is landscape mode the height of the images are squeezed and if it is portrait mode the width of the images are squeezed. 

When all is setup, the method iterates through the images and render them one-by-one on the screen, adjusted a bit along the X-axis to make up for the time that has passed. 
protected override void Draw(GameTime gameTime)
    // Clear the device. (Actually unnecessary since the whole screen will be painted below.)

    // Calculate the "screen width-scale", to allow the app to be drawn both in
    // Landscape and Portrait mode.
    // In Landscape mode, the screenWidthScale will be 1.0 (i.e. original scale)
    // In Portrait mode, the screen must be squeezed.
    var screenWidthScale = (float)GraphicsDevice.Viewport.Width / LandscapeWidth;

    // Calculate the scaled width of one slice.
    var scaledWidth = (int)Math.Ceiling(imageSliceWidth * screenWidthScale);

    // Express the current point in time as "seconds passed since start of app".
    var currentSeconds = (float)gameTime.TotalGameTime.TotalSeconds;

    // Calculate how many seconds that has passed since the current microphone data was captured.
    var secondsPassed = currentSeconds - microphoneDataAppearedAtSeconds;

    // For a smooth move of the pixels, calculate the offset of the current microphone data
    // (where the offset is zero at the time of the new data arrived, and then growing up
    // one full width of a slice.
    var drawOffsetX = secondsPassed * pixelsPerSeconds;

    // Since it is not certain that the next microphone data will come before the current
    // slice has moved its full distance, the offset needs to be truncated so it doesn't
    // move too far.
    if (drawOffsetX > scaledWidth)
        drawOffsetX = scaledWidth;

        // Start draw the slices

        // Start with one slice before the current one, wrap if necessary.
        var imageSlice = currentImageSlice.Previous ?? lastImageSlice;

        // Prepare the rectangle to draw within, starting with the newest
        // slice far to the right of the screen (a bit outside, even).
        var destinationRectangle = new Rectangle(
            (int)(GraphicsDevice.Viewport.Width + scaledWidth - drawOffsetX),

        // Draw the slices in the linked list one by one from the right
        // to the left (from the newest sample to the oldest)
        // and move the destinationRectangle one slice-width at a time
        // until the full screen is covered.
        while (destinationRectangle.X > -scaledWidth)
            // Draw the current image slice.
            spriteBatch.Draw(imageSlice.Value, destinationRectangle, Color.White);

            // Move the destinationRectangle one step to the left.
            destinationRectangle.X -= scaledWidth;

            // Select the previous image slice to draw next time, wrap if necessary.
            imageSlice = imageSlice.Previous ?? lastImageSlice;
        // Drawing done.

The RenderSamples is taking a RenderTarget2D as an argument, which is the texture to be drawn on. The routine iterates through the samples and render them one by one. 
private void RenderSamples(RenderTarget2D target)
        // Redirect the drawing to the given target.

        // Clear the target slice.

        // Begin to draw. Use Additive for an interesting effect.
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);

        // The X-variable points out to which column of pixels to
        // draw on.
        var x = 0;

        // Since the byte-array buffer really holds 16-bit samples, the actual
        // number of samples in one buffer is the number of bytes divided by two.
        var sampleCount = microphoneData.Length / 2;

        // The index of the current sample in the microphone buffer.
        var sampleIndex = 0;

        // The vertical mid point of the image (the Y-position
        // of a zero-sample and the height of the loudest sample).
        var halfHeight = imageSliceHeight / 2;

        // The maximum number of a 16-bit signed integer.
        // Dividing a signed 16-bit integer (the range -32768..32767)
        // by this value will give a value in the range of -1 (inclusive) to 1 (exclusive).
        const float SampleFactor = 32768f;

        // Iterate through the samples and render them on the image.
        for (var i = 0; i < sampleCount; i++)
            // Increment the X-coordinate each time 'samplesPerPixel' pixels
            // has been drawn.
            if ((i > 0) && ((i % samplesPerPixel) == 0))

            // Convert the current sample (16-bit value) from the byte-array to a
            // floating point value in the range of -1 (inclusive) to 1 (exclusive).
            var sampleValue = BitConverter.ToInt16(microphoneData, sampleIndex) / SampleFactor;

            // Scale the sampleValue to its corresponding height in pixels.
            var sampleHeight = (int)Math.Abs(sampleValue * halfHeight);

            // The top of the column of pixels.
            // A positive sample should be drawn from the center and upwards,
            // and a negative sample from the center and downwards.
            // Since a rectangle is used to describe the "pixel column", the
            // top must be modified depending on the sign of the sample (positive/negative).
            var y = (sampleValue < 0)
                ? halfHeight
                : halfHeight - sampleHeight;

            // Create the 1 pixel wide rectangle corresponding to the sample.
            var destinationRectangle = new Rectangle(x, y, 1, sampleHeight);

            // Draw using the white pixel (stretching it to fill the rectangle).

            // Step the two bytes-sample.
            sampleIndex += 2;
        // Drawing done.

        // Restore the normal rendering target (the screen).

The Challenge of the Day

clock March 10, 2012 16:30

Dear fellow developers,

Please create an instance of the following class (using any type as T):

class Foo<T>
    where T : Foo<T>

You may use any technique you like; plain "new MyClass...", using reflection, hacking MSIL, whatever.

Good luck! :)

Follow the rules when developing your Windows Phone app

clock February 9, 2012 16:25

Roughly one month after the first version of Badgers Rock had passed the certification cycle and happily thrived in the Windows Phone Marketplace, I received the following e-mail from one of the app testers: 

Dear Windows Phone Marketplace Developer,

It has been determined through certification testing that the application indicated in the subject line has the following problem(s):
Pass with Notes: Pressing the device back button has no effect on the application.

Steps to Reproduce:
1. Launch the application.
2. Select "PLAY!".
3. Select a stage.
4. Press the device Back button.
5. A pause menu is displayed.
6. Press the device Back button again.
7. Observe the application the application does not respond.

The application passes certification, however the reported issues need to be resolved in the next update to the application.

ACTIONS REQUIRED: Please address the issues listed above, review the Windows Phone Application Certification requirements ( and resubmit your updated application for certification testing.  Future application update submissions will fail certification testing if the same issue(s) are not corrected.
LEASE DO NOT REPLY TO THIS MESSAGE. For further assistance, please visit Developing for Windows Phone 7 forum.


Windows Phone Marketplace Certification

Sam was absolutely right about his finding, but it was in fact a design decision from my side. In the described scenario you are in an ongoing game and then you press the physical Back button on the phone. The game is now paused and you are presented with a couple of options; basically "do you want to end the game or resume?". So, at this point, how should I handle the Back-button? It was of course out of the question to end the game; it's way too simple to accidentally touch this button (depending on how you are holding the device). So making a double miss-tap ending the game would not make any hardcore Badgers Rock-gamer happy. So should the game resume then? This was the only obvious option left (making the game respond to the click), but I didn't really like that anyway because it would not be 100% clear to the player what the Back button would actually do in this case. So I decided to ignore the Back button in this screen and force the player to take an active decision choosing from the spelled-out options on screen -- again, to make it perfectly clear for the player what will happen.

Then this message came from which a couple of things can be noticed:

  • First of all, Microsoft is (for the first time?) really anxious that all the applications are behaving in a consistent way, not only giving guidelines. This is a good thing.
  • Secondly, it's not only automatic testing being performed on your submitted app. This is also a good thing. Although it doesn't seem as every release is tested manually since I had maybe four versions successfully passing the certification cycle when I got this message -- and the behavior in question had been there from the start.
  • Also, at the point in time when I received this message there was no certification phase currently going on; my latest release had already been passed successfully some time ago. Does this mean that the testers are doing the manual testing as a parallel task on top of the (more or less) automatic certification cycle? My app was presumably put in a queue and rested there for a month until someone (Sam!) had time to perform the manual testing.
  • A side note is the not-so-professional link they've included in the e-mail ( This points to a PDF-file you'll have to download that's only telling you that the link has been moved (and of course where to). Suprising? Not really. Elegant? Well, not really...

Of course I should follow the requirements of always having a responsive Back-button!
So now during game-play, the Back-button will basically toggle pause mode on and off.

Badgers Rock

clock February 8, 2012 17:54

Windows Phone Tile


Badgers Rock is a small, completely free (no ads) Windows Phone game I created using the XNA framework during the autumn of 2011.
You can find it on the Windows Phone Marketplace and on Facebook.

The latest released version (V1.4) has both an online high score and cool retro sound.

There was a contest arranged by the "Windows Phone Sweden" Facebook page, that ended on February 3, 2012, and the Winner Niklas Dahlman successfully completed all thirty levels with "three stars" on each levels. Not an easy task considering some levels are really hard and time-demanding to complete. He won a Nokia Bluetooth Stereo Headset BH-111. Congratulations!

The online highscore is provided and hosted by It is free, open source and have a very easy API to integrate. You can choose to host your data yourself or let them do it for you. Really nice.

All the sounds in the game were taken from the Here is a complete list of the sounds that I used in the game.

Unholy characters in code

clock October 8, 2011 17:39

Just a head's up — do try it at home — it's most likely that your co-workers will not approve of these worst-practice naming standards...!


It's generally only when it comes to unit test method names that I allow myself to use underscores. Since those names tend to (and should) be a little chatty, it's nice to separate the words to increase readability. That's the first reason. The other reason is to be able to distinguish between Pascal-cased [variable] names in the sentence, and pure English words. For example; if you don't use underscores it's hard[er] to understand whether the name


means "Checking whether my name is empty" or "Checking whether MyName is empty". Utilizing underscores between the actual words would clear that out.

Now to some funny business. For the faint hearted, please stop reading now!

Exotic characters

Well, underscores are nice enough, but you would go crazy having to read a whole book with them between each word, right? So, giving the fact that modern languages like C# now support foreign characters in addition to the English alphabet, and the fact that the number of charcters in total is rather large, maybe there are some obscure characters for us to use? And as luck would have it; there are. A couple, in fact.

First of all, of course not all unicode characters may be used in a name, just as little as e.g. a #-character would do. But there are a couple of characters that actually appears as spaces on screen, but are valid in type and method names. The character U+1160 (aka the Hangul Jungseong Filler) is one of those. Check this out:

public void Splitᅠnameᅠusingᅠaᅠseethroughᅠcharacter()

Although (or since) this doesn't generate any compiler errors, other developers stumbling across this piece of code might get pretty puzzled...

Please note: your source files will need to be saved in full unicode.

If you think that this is too hard to digest, and insist on having a character you can actually see, you might want to try these babies out:

public void Split﹍name﹍with﹍a﹍dotted﹍underscore()
  // This is character U+FE4D

public void Split﹎name﹎with﹎a﹎dash﹎dot﹎dash﹎underscore()
  // This is character U+FE4E

public void Split﹏name﹏with﹏a﹏wavy﹏underscore()
  // This is character U+FE4F

public void Splitᅳnameᅳwithᅳaᅳcenteredᅳline()
  // This is character U+1173

public void Splitᆖnameᆖwithᆖaᆖcenteredᆖdoubleᆖline()
  // This is character U+1196

public void Splitᆞnameᆞwithᆞaᆞcenteredᆞdot()
  // This is character U+119E

Taking it even worse

If you really want to mess with your co-developers mind, you can even use a character with zero width (i.e. totally invisible). You will actually only notice it when you step it over using the arrow keys; then you'll feel as the key press "didn't take" since passing the character doesn't move the caret.

You can for instance use these weird characters to make it appear as if you are "reusing" the same variable name many times by inserting one or more of these characters in the variable name. One of these invisible characters are U+200B (aka Zero Width Space). In the following example, the zero width space character is inserted 0, 1 and up to 4 times between "my" and "Value" in the different variable declarations. Please note that the code is perfectly legal and will declare five different variables (virtually appearing the same).

public void ReusingVariableNames()
  int myValue = 0; // Actually "myValue"
  int my​Value = 1; // Actually "my+Value" where + is the U+200B character.
  int my​​Value = 2; // Actually "my++Value" where + is the U+200B character.
  int my​​​Value = 3; // Actually "my+++Value" where + is the U+200B character.
  int my​​​​Value = 4; // Actually "my++++Value" where + is the U+200B character.
  // Yes. This will compile nicely...

Quite nasty as they appear the same — but are in fact — totally different names... I guess the only way to make use of this character is in practical jokes...

The full monty

If you decide — against all common sense — that you really want to go for these unholy characters; you will find it pretty difficult since you'll need to copy/paste them the whole time. Well, my friend, there's a cure even for that. Just download and install The Microsoft Keyboard Layout Creator, and you're able to assign one or more of these characters to physical keys of your keyboard.

Upon launch, go into the File menu and select "Load Existing Keyboard...".

 Load Existing Keyboard...

 This will bring up a listbox with all the currently installed keyboard layouts on your machine. Select the one you are using.

Clicking the key-buttons on the main window brings up the small dialog where you can remap the selected key:


Press the "All..."-button to see the full dialog with all the modifier options (+shift/ctrl/alt etc):

Please be aware that there is a restriction of chaning the spacebar key to a non-space character. I tried to map the combination shift+ctrl+space to the "Hangul Jungseong Filler" (the invisible character) but was denied.

Maybe for the best! Tongue out I had to settle with ctrl+shift + the underscore key, which works good enough.

When you're done with your keyboard tweaking, go into the Project menu and select "Build DLL and Setup Package". This action will create a keyboard setup file which you can install on your PC.


After installing the package you can enjoy your new keyboard layout!

Wrapping it up

So, the real use of these exotic characters are maybe just to play some practical joke on someone. Or, one might construct an obfuscator utilizing these characters: imagine renaming all your names into different combinations of the Hangul Jungseong Filler and the Zero Width Space characters. Plain invisible code, my friend! Mohahahaa!

Google Translator Changes Company Names

clock October 2, 2011 17:06

How neutral is really the Google Translator?

At least when it comes to some manufactures of integrated circuits they seems to be quite creative. Try to translate these two sentences from Swedish to English:

Jag föredrar Microchip som företag.
Jag är kritisk mot Microchip som företag.

It will be translated to...

I prefer Microchip as a company.
I am critical of Analog Devices as a company.

...which is aaalmooost a correct translation — apart from the minor detail that the company name has been replaced with one of its chief competitors!
But — worth noticing (or coincidence?) — only in one of the phrases (that happened to have a negative meaning)!


Translating the same Swedish sentences to German shows the exact same phenomena:

Ich bevorzuge Microchip als Unternehmen.
Ich bin kritisch gegenüber Analog Devices als Unternehmen.

But if you then translate these two German sentences back to English you will get another weird name-switch:

I prefer Microchip as a company.
I am critical as compared to Texas businesses.

So, apart from the bad translation of the second sentence from German to English, now "Analog Devices" has been replaced with "Texas". Should that be "Texas Instruments" perhaps? Well maybe... If you take the original two Swedish sentences and replace "Microchip" with "Micrel" (yet another manufacturer of integrated circuits):

Jag föredrar Micrel som företag.
Jag är kritisk mot Micrel som företag.

Translating this to English, now "Micrel" will magically switch to "Texas Instruments" (but now in both sentences):

I prefer Texas Instruments as a company.
I am critical of Texas Instruments as a company.

Two question remains; Who paid Google to replace the names? But the more interesting question is perhaps: What did they really try to accomplish with these mysterious switches?

Conspiracy Now!