-
April 23rd, 2014, 11:52 AM
#1
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
-
April 23rd, 2014, 01:04 PM
#2
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);
}
-
April 23rd, 2014, 01:09 PM
#3
Re: Nullable DateTime in method signature
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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|