Enum.TryParse missing in Windows Phone 7

It’s not present in the WP7 version of mscorlib. Eventually I wrote code like this:

ForumType forumType = ForumType.Unknown;

try
{
    string enumLine = forumTypeInput.Substring(. . .);
    forumType = (ForumType)Enum.Parse(
        typeof(ForumType), enumLine.Trim(), true);
}
catch (ArgumentException)
{
}

Note how I only catch (and swallow) ArgumentException. The call can throw an ArgumentNullException too but that would indicate an application code error, so you should not swallow it. Any other unhandled errors also need to be correctly handled so this is the only exception you should swallow. Kinda surprising they decided to drop TryParse though, since it’s not a particularly heavy method.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s