Nullable DateTime in method signature
Hi,
How do you handle nullable datetimes in method signatures?
Sample code:
Code:
public ViewResult ShipCalendarMonth(int Id, DateTime? dt)
{
if (dt == null) { dt = DateTime.Now; }
var shipCalendar = repo.CruiseCalendarDays(Id, dt, 28);
return View(shipCalendar);
}
I get a red squiggle over the 'dt' parameter in repo.CruiseCalendarDays, because it expects a nonnullable datetime.
How should I approach this?
regards, Guy
Re: Nullable DateTime in method signature
Code:
public ViewResult ShipCalendarMonth(int Id, DateTime? dt)
{
var shipCalendar = repo.CruiseCalendarDays(Id, dt.HasValue ? dt.Value : DateTime.Now, 28);
return View(shipCalendar);
}
Re: Nullable DateTime in method signature
Quote:
Originally Posted by
Arjay
Code:
public ViewResult ShipCalendarMonth(int Id, DateTime? dt)
{
var shipCalendar = repo.CruiseCalendarDays(Id, dt.HasValue ? dt.Value : DateTime.Now, 28);
return View(shipCalendar);
}
Thanks Arjay!
Code:
This also works (just stumbled across it)
public ViewResult ShipCalendarMonth(int Id, DateTime? dt)
{
DateTime startDate = dt ?? DateTime.Now;
var shipCalendar = repo.CruiseCalendarDays(Id, startDate, 28);
return View(shipCalendar);
}