I'm still not satisfied by the readability of the code. The wanted behavior which would be self descriptive would sound like: "I've two fields; one is for the date and one for the hour. This two fields should be in a UserControl of which I have two instances, one is for the event time and one is for the event end time."
My personal opinion is all the extra code is something which is specific to C# syntax, and not to my purpose. Handling null, exceptions, conversions between dates and strings makes me to write that extra code and a developer could get confused and wonder on what was the purpose of that solution. The only thing which is at a functional level in this code is an assignment.
The String.Format
is an example of what I mean... it's a really mean solution. Now that every approach tries to separate Model, View and Controllers, I'm mixing the solution I found to to fill a gap of conversion and the code wrote to make the behavior I needed.
Which solution would you use?
First formulation
public override void OnEventTimeChanged()
{
DateTime eventDateWithTime;
TimeSpan eventTime;
if (TimeSpan.TryParse(EventTime, out eventTime))
{
eventDateWithTime = (EventDate.Date + eventTime);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentUICulture;
EndTime = String.Format(cultureInfo, "{0:HH:mm}", eventDateWithTime);
EndDate = eventDateWithTime;
}
}
Second formulation
public override void OnEventTimeChanged()
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentUICulture;
TimeSpan eventTime;
if (!TimeSpan.TryParse(EventTime, out eventTime)) return;
DateTime eventDateWithTime = (EventDate.Date + eventTime);
string eventTimeString = String.Format(cultureInfo, "{0:HH:mm}", eventDateWithTime);
EndTime = eventTimeString;
EndDate = eventDateWithTime;
}
!
in yourTryParse
at a first glance. (I don't think this is worth an answer, so here's a comment) \$\endgroup\$