I have a DateTime variable that can either be null or a Datetime. I figured a nullable DateTime type would work, but I am getting an error telling me that said
Nullable<DateTime>
does not have a definition for AddDays
. Is there any way to resolve this error?
DateTime? lastInvite = (DateTime?)Session["LastInviteSent"];
if ((string)Session["InviteNudgeFlag"] == "False" && ((lastInvite == null && DateTime.Now >= AcctCreation.AddDays(7)) || (int)Session["InviteCount"] > 0 && DateTime.Now >= lastInvite.AddDays(7)))
{
// Non important code here
}
You should consider trying to make your logic more readable:
var inviteNudgeFlag = bool.Parse((string) Session["InviteNudgeFlag"]);
if(!inviteNudgeFlag)
{
return;
}
var lastInvite = (DateTime?) Session["LastInviteSent"];
var inviteCount = (int) Session["InviteCount"];
var numOfDays = 7;
var now = DateTime.Now;
var weekSinceLastInvite = lastInvite.HasValue
? now >= lastInvite.Value.AddDays(numOfDays)
: now >= AcctCreation.AddDays(numOfDays);
var hasInvites = !lastInvite.HasValue || inviteCount > 0;
var canInvite = hasInvites && weekSinceLastInvite;
if(!canInvite)
{
return;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With