Blogging Tutorials

Posting Source Code on WordPress

I’ve posted a lot of non computer science blog posts recently, but this is supposed to be “the blog of a budding computer scientist!”. So here’s a post which I hope will help a few of my fellow computer science bloggers who use the WordPress blogging platform — which by the way is fantastic.

Quite often when I see blog posts that contain source code it’s formatted in an annoying way, doesn’t have any colour coding or in a worst case scenario is a screenshot of an IDE. It’s impossible for people to copy your code if you take a screenshot of it, and in my experience if you post your code online you want people to copy and adapt it for their own use.

On wordpress you could use <pre> tags in the HTML editor to make code boxes like the following:

//Here's some <pre> formatted code
public static void Main()
      Console.WriteLine("Hello WordPress");

Thats all well and good, it keeps the code seperate from the content of the blog post and gives it a different font and background colour to differentiate it as code, however those of us who are used to working in an IDE, such as Visual Studio, with its syntax highlighting may find it less friendly to read. This is where one of WordPress’ best features comes in.

The [ sourcecode ] tag allows you to post fully colour coded source code in a variety of languages including C#, C++, JavaScript and XML. It also adds some other features like line numbering,  code printing, copy to clipboard and view source — It looks like this:

//Here's some [ sourcecode ] formatted code
public static void Main()
      Console.WriteLine("Hello WordPress");

Much better!

All you have to do is wrap your code like so

[ sourcecode language="LANGUAGECODEHERE" ]
          //Code here
[ /sourcecode ]

Without the space in front of sourcecode (Which I’ve had to put to prevent WordPress from actually making it into a source code box). You then have to replace LANGUAGECODEHERE with the code corresponding to the programming language you are posting:

  • C#’s code is “csharp”
  • XML’s code is “xml”
  • PHP is “php”
  • Java is “java”

You can see all 29 codes at this helpful wordpress help page.

I hope this helps a few of my fellow CS bloggers! 🙂


Programming Tutorials

Using the Tag attribute in XAML or “Where the hell can I bind this!?”

Warning: This blog post assumes some knowledge of XAML and Binding in WPF or Silverlight.

Whilst developing the Hull CS Blogs application for Windows Phone 7 I ran into the same issue several times. Some user interface items, such as Context Menu Menu Items in the Silverlight Toolkit don’t have attributes you can bind properties to in a way that makes sense. Heres an example bit of code to show what I mean

<!--A box containing a list item for the "Latest Blogs" page on the Main Panorama for the Hull CS Blogs app-->
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" Tap="blogTapped">
     <StackPanel Width="411">
          <TextBlock Text="{Binding Title}" Margin="12,-6,12,0" TextWrapping="Wrap" FontSize="30" Foreground="White"/>
          <TextBlock Text="{Binding Author}" Margin="12,-6,12,0" TextWrapping="Wrap" FontSize="20" Foreground="{StaticResource PhoneAccentBrush}" />
          <TextBlock Text="{Binding Abstract, Converter={StaticResource HTMLConvertor}}"  Margin="12,-6,12,0" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="#AFA7A0"/>

In this code we can see that affixing a Tap() event to the Title Textblock, which could then work out which Item was tapped by looking at the binding and comparing it to the Titles of blogs in a linked list in memory would be rather simple. But what if we want the user to be able to touch any of the items in order to activate the Tap() event?

To do this we’re going to have to give the StackPanel a Tap() event. So now, anything inside it that is tapped will trigger that event. Thats great! But how do we work out which item was tapped? Theres nothing we can bind to on a StackPanel that can accept a text input in the same way that the Text attribute can on a Textblock.

Binding the title of the blog post to Name will cause an error whilst Initializing the page, due to how XAML works and binding the title to attributes such as Visbility, Width or AllowDrop both doesn’t work and doesn’t make sense.

Tag attribute to the rescue! According to MSDN “Tag is intended to provide a pre-existing property location where you can store some basic custom information about any FrameworkElement without requiring you to subclass an element.” In essence, Tag is a place for you to store information that doesn’t make sense elsewhere, but you need to be associated with that Element in order for it to function correctly. In our case we can bind our title to it, like so

<!-- Now we can tap our stackpanel 🙂 -->
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" Tap="blogTapped" Tag="{Binding Title}">

All FrameWork Elements have a Tag attribute, so that means most, if not all items in XAML do. So, when you’re thinking “Where the hell do I bind this?!”, just think about tag!


Life Tutorials

Installing Windows 7 using USB 3 – “Cannot Find Required DVD/USB Driver” Error and Solution

As many of you will know from my constant complaining my Dell Inspiron 15R has suffered a gradual but somewhat annoying slow-down and build up of weird bugs — Microsoft Word only opens on the third attempt, after multiple attempts at reinstallation, for example.

Yesterday I decided enough was enough and it was time for some spring cleaning, so I downloaded Windows 7 Professional 64bit from The Univeristy of Hulls Microsoft Academic Alliance portal. Academic Alliance is great, it allows students of subscribing institutions to download and install free versions of Microsofts products including Windows XP, Windows 7, the Windows 8 Beta and parts of the Microsoft Office Suite.

Once my Windows download had finished I started up another bit of Microsoft Software, the “Microsoft USB/DVD Download Tool” which allows you to “Create a bootable DVD or USB flash drive” — In other words make a Windows Install Disk, or in my case an Install USB Flash Drive.

Having installed Windows 7 — and indeed 8 — from USB a few times now the process went pretty quick for me. Select the ISO, Select USB, wait a few minutes, restart the computer, press F12, select boot from USB. Simple right?

Normally, yes, if you put the USB stick in one of the old USB 2.0 ports everything will run through as you expect and you’ll have your shiny new install of Windows 7 in a jiffy — much quicker than burning and installing from DVD.

However if you, like me, for whatever reason plug your USB into one of the blue USB 3.0 ports you’ll run into problems once you’ve selected your language and keyboard type. The error will read “Cannot Find Required DVD/USB Driver”, this is because Windows 7 by default doesn’t have any drivers for USB 3 controllers, and therefore cannot interact with the flash drive in a way it expects to.

At first it took me a while to work out why, and indeed someone less computer literate than myself might not have worked it out — then again I doubt someone less computer literate than my self would be installing from USB 3.0, but i digress. So this blog post is to help anyone who may come across this error.

The fix is simple. Move the USB Flash Drive from the USB 3.0 Port (that’s the blue one) to the USB 2.0 port (that’s the black one).

I assume some other laptops and desktops may encounter similar issues, so i hope this helps a few people — if it does why not let me know in the comments 🙂


Programming Tutorials

Sorting a List of Classes

Today whilst writing my Highscores library, which I’m currently using for Sweepy Cleaner but intend to also use for future XNA games such as those I’ll make at 3 Thing Game, I discovered an awesome feature of lists in C#.

This List.Sort() method, documented here, allows you to sort a list of classes and, because it uses the recursive QuickSort algorithm, its really quick! By default the Sort() method can sort any primitive types, such as strings, ints, float and doubles, without any parameters, which is pretty good but doesn’t allow you to fully utilize the method. This is where the IComparer interface comes in.

An interface, for those who don’t know, is like a rulebook. It says “if you claim to be a comparer, you must include methods with these names, that return these types, otherwise I wont let you claim to be a comparer by stopping you from compiling”.

An example person interface might be like the following

interface Person
string returnName();
int returnAge();
void Eat(string foodType);

This basically says “To be a person you must have a method which returns a string called returnString, a method which returns an int called returnAge and a void method called Eat which accepts a string parameter”. If you miss one of those methods off from your class, which then claims to be a person or you have a returnAge method but it returns a double rather than an int or an eat method which doesn’t accept a string parameter your code won’t compile, so you can see that Interfaces are pretty useful for making sure your classes adhere to a certain rule set. 🙂

The IComparer interface comes in useful, it dictates to you how to make a class which tells the Sort() method how to sort your list of classes.

A class which implements the IComparer interface must have a public int Compare method which accepts two objects in its signature. So if we’re sorting people by age we might have an IComparer like the following.

public class PeopleComparer : IComparer
public int Compare(People person1, People person2)
int returnValue = 1;
if (person1 != null && person2 != null)
returnValue = person2.Age.CompareTo(person1.Age);
return returnValue;

As you can see, our PeopleComparer claims to be an IComparer using the ":" syntax. We then have a method inside the class which returns an integer, with a signature which accepts two People objects. This returns a number based on the CompareTo method which can compare integers, such as our age variable. You could compare height by changing only two words. e.g: returnValue = person2.Height.CompareTo(person1.Height);

(By the way, don’t worry about return value initially being set to 1, this is just to stop the “use of undefined variable” compilation error.)

We can now sort our classes by doing something like

List peopleWhoReadDannyComputerScientist = new List { };
//Imagine this list is filled with 1000 people.
peopleWhoReadDannyComputerScientist.Sort(new PeopleComparer());

The peopleWhoReadDannyComputerScientist list would now be ordered by age, allowing you to do all sorts of cool things, like binary chop search. 🙂

Its as simple as that,