Failed Object Initialization

by Jim Jan 13, 2009 1:39 PM

I was getting this nasty error during the parsing of my XAML:

Failed object initialization (ISupportInitialize.EndInit). Value cannot be null.
Parameter name: element  Error at object 'System.Windows.Controls.ComboBox' in markup file. . .

After (too) much digging around I figured that my selection changed event was being invoked during the parsing of the XAML (which seems a little weird.) As part of the logic in this handler, I’m using VisualTreeHelper to find elements in item templates. And VisualTreeHelper apparently does not like being used on half-parsed XAML.

The solution is easy enough; jump out if the XAML isn’t loaded yet.

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (!this.IsLoaded)
        return;

And put a check at the top of FindVisualChild to make this easier to debug in the future.

private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
    if (!this.IsLoaded)
        throw new InvalidOperationException("Don't call this during loading, it might fail");

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child != null && child is T)
            return (T)child;
        else
        {
            T childOfChild = FindVisualChild<T>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}

Dell D820 Keyboard Replacement

by Jim Jan 11, 2009 8:59 PM

I woke up recently to find the control key on my work machine broken. I couldn’t have been so tired the night before that I would fail to notice a missing control key; the only explanation I can come up with is that the cat must have hooked a claw on in while wandering around in the middle of the night.

DSC_2053

DSC_2055

Ian ordered me a new keyboard and mailed it here – they’re surprisingly cheap.

I always hate when the first step involves prying something open. In this case we need to take off the hinge cover, over the keyboard. You took the battery out first, right? Open the screen all the way. There’s a little slot to insert a flat-blade screwdriver. A little gentle pressure and twisting the screwdriver back and forth, and the edge of the cover pops up. A very easy prying operation – you won’t even put a nick in the plastic.

DSC_2057

There are a couple more clips along the bottom of the hinge cover. Gently pull it up from right to left; again, not a dangerous operation, the clips pop off pretty easily.

DSC_2058Remove the three screws at the top of the keyboard with a small phillips screwdriver.

DSC_2062The aluminum keyboard tray has little protrusions on the edges that hook under the plastic top of the laptop. We want to pull the keyboard up to release the tabs at the bottom, but first the sides need to be freed. A little gentle prying with the screwdriver does the trick, and we’re throwing this one away (or keeping it for the spare keys,) so no big deal if we tweak it a bit. 

DSC_2063

Fold the keyboard down towards the front of the computer – there’s just one plug that attaches it.

Pull up on the blue tab to disconnect the keyboard.

DSC_2065

Assembly is the reverse, of course. The only tricky part is getting the rather flimsy keyboard back in without bending it. I used the screwdriver to push the edges down, where it clips under the cover on the sides.

It was interesting to see the design of the keyboard – it’s completely sealed with plastic on the bottom, and the edges are turned up, like a tray. So next time you dump a Corona over into your laptop keyboard, don’t freak out! It’s not going anywhere :-)

The C# as Operator

by Jim Jan 01, 2009 9:24 PM

It’s code review time!

The as operator is not the same thing as a cast. It is not better, it is different. It is appropriate in some situations, but not in others. Some people us it because they prefer the syntax, but it does different things, and the two are NOT interchangeable.

Using the as operator differs from a cast in C# in three important ways:

  1. It returns null when the variable you are trying to convert is not of the requested type or in its inheritance chain, instead of throwing an exception.
  2. It can only be applied to reference type variables converting to reference types.
  3. Using as will not perform user-defined conversions, such as implicit or explicit conversion operators, which casting syntax will do.

Number three is a good enough reason to prefer the cast as a general rule. By using the as operator you may be circumventing functionality that someone intentionally put into the class you’re referencing!

Here is a hard and fast rule for you:

IF YOU AREN’T GOING TO CHECK FOR THE NULL, DON’T USE THE as OPERATOR.

If there is a bug that results in the type you’re converting being the wrong type, you’ll get a null reference exception – possibly in code that is far from the as operator you used. Someone will have to debug that one day. And they’re going to waste time trying to figure out why the value is null, when they really need to be trying to figure out why the value is of the wrong type.

If you’re going to assume that the object is of a particular type, use the direct cast. This will result in an invalid cast exception, which is informative, and happens right there at the cast. The stack trace and the error message will tell someone precisely what happened and where:

System.InvalidCastException: Unable to cast object of type 'Foo' to type 'Bar'.

Now we know what went wrong – a NullReferenceException doesn’t tell us that.

Tags: