Nat Luengnaruemitchai

Geek Blog

  Home  |   Contact  |   Syndication    |   Login
  93 Posts | 0 Stories | 141 Comments | 243 Trackbacks

News

Archives

Post Categories

Blogroll

If you extensively use Windows Form, somehow you may experience Data Binding with Custom Objects before. At first it seems to be a wonderful experience when I discovered that it's relatively easy to bind data to custom objects. However, after a while, I found that it could not handle null properly. If a field in the custom object is a reference type and has a value = null, sometimes, winform will just throw an exception (NullReferenceException from Binding class). Have you ever experience the same problem? How did you deal with that? Do we have enough noise to get it fixed?

Updated: Although I understand that we can handle null by hooking into OnFormat and OnParse, I believe that this is very troublesome as many developers are not familiar with this method. Also I believe that it does not make sense to do that manually for every fields in the binding. Plus, at least .NET CLR should throw more specific exception rather than just NullReferenceException which leaves developers in cold to figure out what happened exactly.

posted on Sunday, February 20, 2005 12:29 PM

Feedback

# re: Will Microsoft Fix A Nasty Null Problem In .NET Data Binding? 2/20/2005 1:40 PM Paul
Until .NET 2.0 arrives with nullable type support, you should look into handling the Binding class Parse and Format events.

In the format event, you can check to see if the item is null and if it is provide an empty string. Checking for null would work along the same lines as what you get when using a typed dataset (e.g. member variable is of type object which can be null, and you'd have properties to test if it is null, set to null, and retrieve the typed property value if it isn't null).

Feel free to contact me directly if you need some more guidance.

# re: Will Microsoft Fix A Nasty Null Problem In .NET Data Binding? 3/3/2005 10:11 PM Scott Bellware
Some would say that what you're facing is a design problem rather than a tool problem. It might not be such a good idea at all to bind directly to business objects. Maybe it is, maybe it isn't. I've been doing a bit of reading lately on testable UI and test-first UI. One theme keeps coming up - keep domain layer objects out of the UI, provide an intermediary "View" layer, handle UI events in the intermediary. Interesting stuff. Google "humble dialog". I'd love to hear any ideas you have on this stuff if you ultimately get some time to take a look.

Cheers.

# re: Will Microsoft Fix A Nasty Null Problem In .NET Data Binding? 3/4/2005 7:15 AM Nat
It's not actually a design problem. Basically, .NET Data Binding should handle null probably. However, if you look inside the code, when trying to format data into string for presentation purpose, .NET BCL does not check whether the value is null or not before trying to convert that into string. If the code just check for null and return null properly. Everything would work nicely.

Post Feedback

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