Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

01 Nov 2009

Another method i wrote once too many

Virtually every Silverlight application will fetch resources at one point or another. In case you’re using the WebClient you have probably written the following in your xxxCompletedEventHandler

if (e.Error != null && !e.Cancelled)
{
	// do something with the result
}

Anyway, i don’t like repetition so i captured the conditions in a method:

public static class ExtensionMethods
{
	public static bool HasResult(this AsyncCompletedEventArgs e)
	{
		if (e.Error != null) return false;
		if (e.Cancelled) return false;
		return true;
	}
}

And now we can write our code as:

if (e.HasResult())
{
	// do something with the result
}

Apart from saving a couple of keystrokes this also allows us to easily add another condition to determine the success of the operation.