Friday, April 04, 2008
Lessons learned - Little new ones, big old ones
We’ve got an in-house application that we’re working on as a training exercise. Like most projects of this type we find ourselves relearning old lessons.
We wanted to extend the functionality of the standard windows DateTimePicker control by creating a new composite control with a DateTimePicker and adding spinners for hours/minutes. We started out with the DateTimePicker and two spinners (pretty obvious first step) and gave the class a DateTime property to hold the core DateTime value. We then data bound the DateTimePicker and the two spinners to the DateTime property. The DateTimePicker was bound to the DateTime property and the spinners were hooked up to the hour or minute components of class’ DateTime property.
We wired everything up and tried running the shell application. Every time we touched the composite control we got exceptions revolving around one of the constituent controls (dtPicker or spinner) having a null value. Mike Yeager wandered in and made the fateful observation that we had “grossly over engineered” the composite control by having each constituent control separately data bound to the same property. After all, the entire DateTime value was stored within the dateTimePicker control.
We removed the databinding of the two spinners to the DateTime property and created properties for hours and minutes that were based on the existing hour/minute components of the dateTimePicker. The getter of the hours/minutes properties returned the hour or minute component of the DateTime value in the dateTimePicker. The setter of the properties called a method that concatenated the year/month/day components of the dateTimePicker with the values of the hour and minute spinners into a new DateTime value. The spinners were hooked up to the hour and minute properties.
It took about 15 minutes to scrap the old code, reassemble the controls and properties, and then bind everything together. It worked on the first try!
- The “new” lesson learned was specific and almost trivial: When extending the functionality of a multi-faceted control, use that control’s internal value as the basis for your work rather than reinvent the (DateTime value) wheel by pushing three separate controls at one value.
The “old” lesson is: “Think about what you’re going to do before you even touch the keyboard. You should have a clear vision of your objective before writing the first line of code."
Claudio will be amplifying on this in his blog, complete with code samples.
Posted @ 4:13 PM by Chichester, Thom (tchichester@eps-software.com)
Post a Comment:
Comment Title (required):
Your Name (optional):
Your Email (optional):
Your Web Site (optional):
Your Comment (required):