# Thursday, April 30, 2009

The source of the aporkalypse has been identified....

 

posted on Thursday, April 30, 2009 8:16:04 PM UTC  #    Comments [0] Trackback
# Tuesday, April 07, 2009
Yes, it's true; the Shirley Temple Pilots are going to be playing for the first time in ages at Finns in Weymouth on Sat 11th July. It'll be loud.... ;)
posted on Tuesday, April 07, 2009 10:12:12 PM UTC  #    Comments [0] Trackback
# Friday, April 03, 2009

Are Microsoft really starting to turn a page when it comes to Open Source? Let’s hope so; they’ve just released the source code to the (excellent) ASP.NET MVC framework under the OSI-approved MS-PL license. There's more information over at Scott Guthrie’s blog – I’d particularly recommend reading his ASP.NET MVC PDF tutorial as it gives an excellent overview of ASP.NET MVC itself.

posted on Friday, April 03, 2009 7:11:17 AM UTC  #    Comments [0] Trackback
# Wednesday, March 25, 2009

One of the nice features introduced into Java 1.5 that is missing in C# is enum types. In C#, enums are nothing more than a defined set of values with labels, whereas in Java enums can contain methods. I've seen a few attempts to mimic Java enums in C#, but most solutions seem to rely on Attributes to decorate the enum which is fine except that in relative terms, reflection operations are expensive.

C# 3.0 introduced the concept of extension methods which enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. As is my wont, I was thinking about this in the shower this morning when I had a eureka moment - surely it should be possible to fake Java enums using C# extension methods?

Using the Java planets example, it didn't take long to whip up the following:

using System;
using System.Collections.Generic;

namespace ScratchPad
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var p = new Program();
            p.Run();
        }

        private void Run()
        {
            double earthWeight = 175;
            double mass = earthWeight / Planet.Earth.SurfaceGravity();

            foreach (Planet planet in Enum.GetValues(typeof(Planet))) {
                Console.WriteLine("Your weight on {0} is {1}", planet, planet.SurfaceWeight(mass));
            }
        }
    }

    public enum Planet
    {
        Mercury,
        Venus,
        Earth,
        Mars,
        Jupiter,
        Saturn,
        Uranus,
        Neptune
    }

    public static class PlanetExtensions
    {
        private static readonly Dictionary<Planet, PlanetData> planetMap = new Dictionary<Planet, PlanetData>
          {
              {Planet.Mercury, new PlanetData(3.303e+23, 2.4397e6)},
              {Planet.Venus, new PlanetData(4.869e+24, 6.0518e6)},
              {Planet.Earth, new PlanetData(5.976e+24, 6.37814e6)},
              {Planet.Mars, new PlanetData(6.421e+23, 3.3972e6)},
              {Planet.Jupiter, new PlanetData(1.9e+27,   7.1492e7)},
              {Planet.Saturn, new PlanetData(5.688e+26, 6.0268e7)},
              {Planet.Uranus, new PlanetData(8.686e+25, 2.5559e7)},
              {Planet.Neptune, new PlanetData(1.024e+26, 2.4746e7)}
          };

        private const double G = 6.67300E-11;

        public static double Mass(this Planet planet)
        {
            return GetPlanetData(planet).Mass;
        }

        public static double Radius(this Planet planet)
        {
            return GetPlanetData(planet).Radius;
        }

        public static double SurfaceGravity(this Planet planet)
        {
            PlanetData planetData = GetPlanetData(planet);

            return G * planetData.Mass / (planetData.Radius * planetData.Radius);
        }

        public static double SurfaceWeight(this Planet planet, double mass)
        {
            return mass * SurfaceGravity(planet);
        }

        private static PlanetData GetPlanetData(Planet planet)
        {
            if (!planetMap.ContainsKey(planet))
                throw new ArgumentOutOfRangeException("planet", "Unknown Planet");

            return planetMap[planet];
        }

        #region Nested type: PlanetData

        public class PlanetData
        {            
            public PlanetData(double mass, double radius)
            {
                Mass = mass;
                Radius = radius;
            }

            public double Mass { get; private set; }
            public double Radius { get; private set; }
        }

        #endregion
    }
}

It's all very simple; the only drawback is that you must remember to match any changes to your enum in the associated extension class. Overall, I think this is a pattern I might start to use more often.

posted on Wednesday, March 25, 2009 10:23:36 AM UTC  #    Comments [0] Trackback
# Friday, March 13, 2009

It’s funny how little things can really get under your skin and annoy you; at work, I use a combination of Windows XP and Windows 2003 Server for development and day to day work which is fine, but as I use Vista at home I find that I miss the UI enhancements more and more.

Unlike a lot of people, I seem to have had a rosy experience with Vista; maybe because I didn’t have unrealistic expectations about the OS (I wouldn’t dream of running it on some of the older machines I’ve seen it shoehorned into), and I’ve had some reasonably up to date hardware to run it on. The only real issues that I had with the OS were very early in its life, when some of the third-party drivers for my hardware devices were less than stable – my Hauppauge TV capture card was a prime example. Nowadays though I struggle to find any such issues, even though I’m running the x64 version of Vista.

My particular bugbear at the moment is the XP (and Windows 2003) Start Menu; I’ve got so used to the convenience of the Search function in Vista that not having it available is like having an arm tied behind my back. The amount of times I find myself instinctively pressing the Windows key and then typing “Visual” to find Visual Studio on my development box is ridiculous.

What’s even more annoying is that my work laptop was originally purchased with Vista, which was when downgraded to XP by the IT department...

posted on Friday, March 13, 2009 12:20:24 PM UTC  #    Comments [0] Trackback