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.
Hehe, nice i’ve also created an extension method to remove these repeated checks. I guess it’s a common/helpfull way.