Category Archives: Debugging

The Danger of Null Values

I made a goof ball mistake today.  I was using LINQ to query a List<> and return the ToString() of the result.  As usual I was using FirstOrDefault() and I simply added .ToString() to the end of it.

fieldValue = (from d in customFields where d.CustomFieldId == fieldId select;

Of course things worked swimmingly until the Default (null) was hit. At that point I got an Object not set to an instance error.  I had to laugh at myself.

var fieldData = (from d in customFields where d.CustomFieldId == fieldId select;
fieldValue = fieldData != null ? fieldData.ToString() : "";

Collection Was Modified; Enumeraton Operation May Not Execute

Some Background

This was a very interesting bug for me to track down.  Initially it did not bubble up as the error that is in this title.  Initially it revealed itself as an out of bounds error when I tried to set IsOpen to true for a ContextMenu in a Silverlight project.  After lots of debugging I finally landed on this culprit.

The Task

The task I was trying to accomplish was pretty straight forward.  Given a List<T> remove any duplicate entries based on a given object.  Again, a straight forward task, or so I thought.

foreach(MyClass item in myListOfMyClass)
    if(item.SomeValue == objectOfMyClass.SomeValue)

Read the rest of this entry