Friday, September 11, 2009

Exceptional Data

As I begin this blog entry, it is early in the day, so I do not yet know if this will be an exceptional day.  Nevertheless, I would like to talk about exceptions.

All exceptions have a Data property, which can be very useful.  I cannot say whether or not Microsoft uses it for any of their exception handling, say, for example,  within their applications such as Excel, or within the code for their .NET framework.  However, I can say that I have never seen it used in any projects I have been involved with over the last seven years in the .NET world.

Notice the Data property in the following listing for the ‘ex’ variable in the Watch1 window:

WatchWindow

It is obviously empty in this case.  However, observe that it is a free-form dictionary.  It has

a Keys collection and a Values collection.  The elements of both are of type object.  Thus you can add just about anything that you feel might be useful to this dictionary.  (NOTE:  If this illustration or any other is too small for you to read, just double-click it to temporarily view it in a separate window.  Then navigate back to the original page.)

To illustrate the use of the Data property, I have, believe it or not, developed a simple application that brews beer.  A brewing application must have a Beer class, so here it is:

Beer

Since I have not yet perfected the brewing process, the Brew method throws an ApplicationException.  To assist with determining what the error might be, the Brew method adds the value of the makeDark local variable and the Beer instance that caused the exception to the Data dictionary.

The main program, which defined the beer and initiated the brewing process, needs to collect this information, so that I can achieve perfection:

Brewing

Here is the output:

BrewOutput

Now, you might say that I could have developed a custom BrewingException class which included the two elements that I placed in the dictionary.  This is true.  However, even when using a custom exception class, I am sure you can imagine situations where you might like to collect additional information.  This is a perfect job for the Exception class’s Data dictionary.

I hope you have an Exceptional day!

Friday, September 4, 2009

Feeling List<>-less?

Today I responded to a question on the Microsoft Visual C# Developer Center. The person (I’ll call him Mr. X) wanted to break a generic list of objects into separate generic lists, where each of the new lists contained entries with a common value for a particular property. He wanted to combine these new lists into a list of lists. I responded that he might consider creating a dictionary instead of creating a list of lists, but that, if he really wanted a list of lists, he could use the dictionary to create it. Let’s look at the details of how we might accomplish this. (Note: if an image below is too small for you to read, just double-click it. Then, when you are done viewing it, just navigate back to the blog page.)

Mr. X was working with a CarType class consisting of four elementary properties. Starting from a list of CarType objects,
CarType

he wanted to break this into separate lists, where the entries in each list have a common value for ID.

So … the first thing we do is to create the original list: LoadCarTypes

Then we create a dictionary of type Dictionary<int,List<CarType>> and load it in a simple foreach loop. Note that when we

LoadDictionary

encounter an entry in the original list for which there is no existing list in the dictionary, we first create the requisite new list.

If we are doing this in a console application, we can verify that the dictionary lists were properly created with the following two-level loop:

DictionaryValidation

Note that each entry in the dictionary is of type KeyValuePair<int,List<CarType>>. This object enables us to easily retrieve the key (int) and the corresponding value (List<CarType>) of each dictionary entry.

If we wish to put the final results in a list of lists, we just create the list of lists and dump the dictionary contents into it as follows:

ListOfLists

The validation is similar to that for the dictionary. Here is the final result, expressed in the Main method of a console application:

ListListProgram

Go ye forth and make lists!