Monthly Archives: September 2009

Just banned IE6

Hi all,

Unfortunately for all my blog readers stuck on IE6, I decided to leave IE6 without water and bread to help it die :) . In plain English: visitors to my blog using this crappy browser no longer get access to my blog, instead they are facing a nice-looking popup, asking them nicely if they already know to be outdated (and how to become alive again :) ).

The idea comes from http://code.google.com/p/ie6-upgrade-warning/. As a programmer, I decided to tweak the script a little to provide a better looking popup for my beloved IE6 visitors (statistics state that they are responsible for about 7% of my site visits, when I am writing this post – something to keep in mind when designing the get-alive-screen :) ).

What did I change?

  1. I added some scripting to disable select boxes below my popup (guess: a nasty IE bug keeps popping them over my popup, which troubles the IE6 view on my blog).
  2. I also changed the background to #ccc for a more relaxed view.
  3. For the same reasonĀ  I also changed opacity around the popup to 75% (just enough to see how interesting the blog could be, should they not have IE6 anymore).

Have a look at the result:

Bye IE6!

You can download the source code for this goal on the website mentioned above. The altered javascript file is located here: warning.js. Good luck banning IE6 :) !

The using statement and XmlReader.Create

Are you also fond of using the using statement (e.g. when dealing with streams)? I am, since it kind-of guarantees me that streams are closed when I think they should be closed, even if I do not specify the Dispose() command. You probably noticed the ‘kind-of guarantee’ part. You are right, this is not always the case. It took me a while to find out the the following is not going to work as smooth as expected:

using (var reader = XmlReader.Create(File.Open(fileName, FileMode.Open)))
    { ... }

You probably wonder what’s the problem of the code printed above. I don’t see it too, however, I know there is a problem related to this code. The problem lies in the Create overload used in the example code. It uses default XmlReaderSettings. Unfortunately, this settings do not include having the CloseInput property set to true. You can do the math: no closed file at all! That’s a guarantee for having troubles on the file ‘being in use already’.

The solution to this problem is quite simple:

using (XmlReader reader = XmlReader.Create(File.Open(path, FileMode.Open),
         new XmlReaderSettings { CloseInput = true })) { ... }

How can we call this? A ‘bug’? No, at least not technically spoken. Since the XmlReader creates the stream, it should close it too. The case is, you should not expext it knowing the using statement as most people know it (fail-proofing your code). Since I know this, I am a little bit scared about using using statements without calling the stream’s constructor. You can do so, but be sure it does what you expect it to do… In my opinion, the using statement still remains very helpful, please I do not consider it being as perfect as I thought it to be for years ;)

Calculate age (for instance, on reference data)

Now and then, I have to calculate ages (for instance, on a reference date). The method to calculate ages in .NET is pretty simple, although it can be ‘a pain in the ass’ to find out how simple it should have been :) … Enjoy the code :) !

public static class DateTimeHelper
{
    public static int CalculateAge(DateTime birthday)
    {
        return CalculateAge(DateTime.Now, birthday);
    }

    public static int CalculateAge(DateTime referenceDate, DateTime birthday)
    {
        // calculate age in years on the given reference date.
        var comparisonDate = new DateTime(birthday.Year, referenceDate.Month, referenceDate.Day);

        return (comparisonDate.Date < birthday.Date)
                      ? referenceDate.Year - birthday.Year - 1
                      : referenceDate.Year - birthday.Year;
    }
}