June 2008 Entries


Virtual CDRom Control Panel - Mount Failed

I use the Windows XP Virtual CDRom Control Panel regularly.  It's a great utility for mounting .iso files to a virtual cd rom drive to avoid having to burn them to an actual disk.  Many large development tools (especially from MSDN) are distributed in .iso format for download.  However, one of the most frustrating errors with the tool is the ambiguous "Mount Failed" exception that is sometimes thrown. I received that error this morning while trying to mount the Visual Studio 2008 Pro image.  I remembered running into this exception on numerous other occasions but couldn't remember the exact cause.  After a small amount of googling...

JavaScript Print Button

To implement a print button on a web page, you can use the window.print() javascript function.  The following example shows an image that is displaying a printer icon and will print the current page when clicked. Code: <img border="0" onclick="window.print();" src="/images/PrintIcon.gif" style="cursor:hand;" /> (Replace the text /images/PrintIcon.gif with the location of an icon in your site.) Example: A better application of this idea is to first create a page that renders a print preview of the information you want to print.  This print preview will look similar to the original page but will not contain navigation items, header images, or other page clutter.  Furthermore, the data can be formatted...

Count > 0 vs Count != 0

I came across an issue today that I think perfectly exhibits the principal of forward thinking in coding.  Consider the following block of code: IList<MyObject> myList = myAdapter.Read(); if (myList.Count != 0) {     ... do some work ... } The if statement is only executing its contents if the count of objects in the list is not zero.  This seems pretty harmless on the surface.  We're taking an IList<T> after all and we know that List<T>.Count returns zero if the list is empty.  Therein lies the trap.  We're not necessarily talking about List<T> here, we're talking about any class that implements the IList<T> interface.  You can see the facepalm coming...

Yield - Enumeration Candy

As we all know, I'm a little slow on the uptake.  This time I managed to miss a really great keyword in C#: yield.  The combination yield return is a lovely piece of C# candy that can be used to simplify a common function.  Consider the following section of code: private IEnumerable<MyObject> FindObjectsWithThreeChildren(IEnumerable<MyObject> sourceList) {     IList<MyObject> results = new List<MyObject>();     foreach (MyObject obj in sourceList)     {         if (obj.Children.Count == 3)             results.Add(obj);     }     return results; } We've all written something along those lines before.  Build a collection up based upon some predicate and then return it.  Now yes, this could actually be solved much more...