Creativity, Innovation... Failure



VS Intellisense

Here's a very interesting blog entry I found about how Visual Studio's Intellisense works and some of its seemingly bizarre behaviors..

http://blogs.msdn.com/cyrusn/archive/2005/05/13/417344.aspx


Detecting design mode

Lets say you've just created a user control or customized an existing control from the .Net Framework and you want the bahavior to be different when you're in design mode vs execution mode. How can you do it?

Solution #1: DesignMode Property 

The first an very intuitive approach is to use the DesignMode property of your control. The problem is that it will always return false if it has a parent... not so great

Solution #2: Design Mode Property with a twist 

The solution then is to go up the control hierarchy and find the parent with the right value!
The resulting code will look a little like this:

public bool IsDesignMode ()
{
    Control instance = this;
    while (instance != null)
    {
        if (instance.Site == null) return false;
        if (instance.Site.DesignMode) return true;
        instance = instance.Parent;
    }
    return false;
}

Using this method can prevent errors where the GUI does not draw properly in design mode because it tries to access data (i.e. database connection)
The biggest limitation is that you can't call the method from the constructor because the parent may not be set yet!

Of course there are other solutions but they are not as elegant. Here's one of them:

bool designMode = Application.CompanyName == "Microsoft Corporation";

This solution works from the constructor, so it does have its advantages...


Suggested Reading: The Timeless Way of Building

This book written by Christopher Alexander some 30 years ago has a priori nothing to do with programming. Through the book he explains his vision of patterns in architecture, how to give certain qualities to buildings; how to make them (and us) feel alive, whole.. beautiful... or 'have' as he puts it: the quality without a name.

Beauty, purpose, life and wholesomeness are some of the qualities that we, as programmers, also strive to achieve when we are coding.

I must say that the author makes some very interesting points that can be applied to many areas of your life!

A wonderful book to appreciate and to read again and again! 

Here's a link:
http://www.amazon.com/Timeless-Way-Building-Christopher-Alexander/dp/0195024028