Archive for August, 2007

Fun with Extension Methods and Enums

Extension methods are one of my favorite features in C# 3.0. Tim Rayburn has already posted something about how we can use extension methods on interfaces to do some cool things, allowing us to sort of provide some implementation code to interfaces.

Yesterday I was implementing something in a project that involved an enum, and was wandering whether extension methods also apply to enums; and they do!

Here’s the scenario: I have a ComparisonOperator enum that I use to keep a list of comparison operators that may be used to create where clauses on the fly. This enum looks something like this:

    public enum ComparisonOperator

In places where I use this enum, I need to convert values to their SQL equivalent. For instance, Equals should translate to "=", LessThan should translate to "<", and so on.

In .NET 2.0 I have created a helper method somewhere with a switch statement that does the translation, since enums cannot have their own methods. In .NET 3.5, though, I can create an extension method like so:

    public static class EnumExtensions
        public static string ToSql(this ComparisonOperator token)
            string sqlEquivalent = String.Empty;
            switch (token)
                case ComparisonOperator.Like:
                    sqlEquivalent = "LIKE";
                case ComparisonOperator.Equals:
                    sqlEquivalent = "=";
                case ComparisonOperator.NotEquals:
                    sqlEquivalent = "!=";
                case ComparisonOperator.LessThan:
                    sqlEquivalent = "<";
                case ComparisonOperator.GreaterThan:
                    sqlEquivalent = ">";
            return sqlEquivalent;

The ToSql method extends the ComparisonOperator enum, by providing the translation between the enum value and the SQL equivalent string. Now I can easily use my enum and call the extension method on it, like so:

            ComparisonOperator likeOperator = ComparisonOperator.Like;
            ComparisonOperator notEqualsOperator = ComparisonOperator.NotEquals;
            ComparisonOperator lessThanOperator = ComparisonOperator.LessThan;

            Console.WriteLine(likeOperator.ToSql());  // prints LIKE
            Console.WriteLine(notEqualsOperator.ToSql()); // prints !=
            Console.WriteLine(lessThanOperator.ToSql()); // prints <

Of course, this is something that not every developer would understand, because nobody expects an enum to have methods, but things like this will begin to show up as people get more used to the new features in .NET 3.5.

Leave a comment

Visual Studio and the messages that irritate the living heck out of me

Oh boy, some messages that we get from Visual Studio are maddening.

Here I am trying to update a project to use the latest version of some third-party assemblies, and I get the following error in Visual Studio when I try to build the solution:

Hmm, that doesn’t give me much. Let’s try to double-click the message, and see if that helps… this is what I get:

Nope, that does not help at all. What document? What assembly? Give me something!!

But this next one is priceless:

hmmm… let me see… Visual Studio is Busy. Hmpf… really? No kidding… SO AM I!!!

But wait, there’s another one that even better. Sometime last year I was having some trouble with some queries in SQL Server, and decided to use the Tuning Advisor. I open Management Studio, click the “Tuning Advisor” button, and after a few seconds I get a message that says the following (I’ve printed it out at the time – I still have it here on my desk – but didn’t take a snapshot, so I’ll just type in the message here):


Microsoft Visual C++ Runtime Library

Runtime Error!

Program: c:\Progr…

– Attempt to initialize the CRT more than once.
This indicates a bug in your application.


How about that? It indicates an error in MY application? Really?? This one cracks me up every time I read it.  🙂

Leave a comment