Steven's profileDevelopmentalBlogLists Tools Help

Blog


    29 August

    DEVSTA - The programming competition for star developers!

    Although it hasn't been officially announced by Microsoft yet, the information was revealed on the OzSilverLight mailing list today that there will be a programming competition for Australian developers starting in September.

    Here is the URL: http://devsta.microsoft.com.au/

    What's really interesting about this particular competition is that it is offering some seriously cool prizes! For instance, first place is an all expenses paid trip to Mix09 in Vegas next year, an XBOX Elite plus games, and a MSDN subscription.

    The "theme" of the competition will be announced on 29th September, and then it will be open slather. Developers will have exactly 200 hours and 8 minutes to complete their submission for the competition.

    So start stocking that jolt cola and be ready on 29th Sept to compete to become the next DEVSTA!

    20 August

    System.IConvertible - Its not a Ferrari

    You might be familiar with the System.Convert class, which has a number of methods for converting from/to different types. For example:

    short s = Convert.ToInt16(int.MaxValue);

    We live in a world of casting and you might find this code somewhat pointless. After all, it will still raise StackOverflowException and lose precision with floating poing conversions, so what's the point?

    The convert class is capable of converting an instance of ANY type into one of the common base types (sting, int, ulong, char, etc). If a type implements the System.IConvertible interface, it can define how conversions of that type will occur.

    Lets consider a (not so practical) example. Say we have a product class with the following properties:

    public int ProductId { get; set; }
    public DateTime DateAvailable { get; set; }
    public string Name { get; set; }

    Our Product class also has a parameterless constructor. In this example, we also make the Product class implement the IConvertible interface. This forces us to implement 16 methods and 1 property. Those methods all relate directly to conversion. For example, here's the implementation of one of those methods:

    public ulong ToUInt64(IFormatProvider provider) {
          return (UInt64)ProductId;
    }

    What we are saying here is that if someone tries to convert our Product instance to a ulong then it will simply assign the ProductId. Our test code looks like this:

    Product p = new Product { Name="Car", DateAvailable=DateTime.Now, ProductId=7 };
    UInt64 i = Convert.ToUInt64(p);
    Console.WriteLine(i);

    The output is the number "7" because that is how our implementation of IConvertible tells the Convert class to handle conversions to UInt64.

    Simple eh?