Steve Michelotti

C#, ASP.NET, and other stuff

  Home  |   Contact  |   Syndication    |   Login
  45 Posts | 1 Stories | 130 Comments | 52 Trackbacks

News



Archives

Post Categories

Image Galleries

Articles

Blogs

In my last post, I discussed creating a static class for Parsing nullable types:

http://geekswithblogs.net/michelotti/archive/2006/01/16/66014.aspx

However, 2.0 also introducing a new TryParse() pattern so that developers would not have to rely on catching exceptions when attempting a Parse() method.  For example:

http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx

We can incorporate the TryParse pattern into our NullableParser class as well so that our consuming code to look something like this:

DateTime? defaultDate = DateTime.Now;

NullableParser.TryParseNullableDateTime(s, out defaultDate);

person.DateOfBirth = defaultDate;

Internally, we can implement this the same way as the ParseXXX() methods by leveraging delegate inference and generics.  First define the delegate:

private delegate bool TryParseDelegate(string s, out T result);

Now define the private generic method:

private static bool TryParseNullable(string s, out Nullable result, TryParseDelegate tryParse) where T : struct

{

  if (string.IsNullOrEmpty(s))

  {

    result = null;

    return true;

  }

  else

  {

    T temp;

    bool success = tryParse(s, out temp);

    result = temp;

    return success;

  }

}

Now each public method is trivial to implement:

public static bool TryParseNullableDateTime(string s, out DateTime? result)

{

  return TryParseNullable<DateTime>(s, out result, DateTime.TryParse);

}

posted on Monday, January 16, 2006 6:35 AM

Feedback

# re: TryParse for Nullable Types 10/10/2006 8:15 AM David Nelson
When I try to compile this I get a series of compiler errors the first is: "Constraints are not allowed on non-generic declarations". If I then comment out the constraint I get "The type or namespace name 'T' could not be found". After a series of changes to make this compile I eventually get "'out System.Nullable': static types cannot be used as parameters"

Maybe I'm missing something? Has anyone actually gotten this code to compile?


# re: TryParse for Nullable Types, Corrections 10/10/2006 8:20 AM David Nelson
Eureka!
I finally got this to work, the following is the new declaration that will allow the code to compile.

public delegate bool TryParseDelegate<T>(string s, out T result);

public static bool TryParseNullable<T>(string s, out Nullable<T> result, TryParseDelegate<T> tryParse) where T : struct

# re: TryParse for Nullable Types 1/15/2007 4:42 PM JH
Unit testing proved that there was a difference between calling Decimal.TryParse versis the generic TryParseNullable method. Here is the correction that makes it return the same results as if it were being called directly:

public static bool TryParseNullable<T>(string value, out Nullable<T> result, TryParseDelegate<T> tryParse) where T : struct {
if (string.IsNullOrEmpty(value)) {
result = default(T);
return false;
} else {
T temp;
bool success = tryParse(value, out temp);
result = temp;
return success;
}
}

# re: TryParse for Nullable Types 9/25/2007 2:48 PM RK
Just ran across this and had to comment, I know it's a few months behind.

The point of this utility is not to mimic the .TryParse() method exactly, but to provide some simple methods that will allow nullable types to paly nice with UI.

I can see how one might have a need for utilities to parse data into nullable types, and would want the parsing routines to behave exactly like the built-ins. But then the question is why are you using nullable types in the first place? The TryParse is designed to return the default value if it is passed a null or an invalid string.

My $.02

# re: TryParse for Nullable Types 10/10/2007 12:51 PM Toby
in response to RK:
nullables are handy in a variety of situations but in my case, I'm using nHibernate and it can map a null data member to a dbnull in the database and this code gives me a clean way of handling the conversion from the UI.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 7 and type the answer here: