Trying to subtract days form a given date C#

Hello,
I’m trying to subtract 3 days from a given date with the following code in a Method Directive

I keep getting the following error

“‘DateTime?’ does not contain a definition for ‘AddDays’ and no accessible extension method ‘AddDays’ accepting a first argument of type ‘DateTime?’ could be found (are you missing a using directive or an assembly reference?)”

Can anyone see the missing rung on the ladder??

var poDueDate = ttpo.DueDate;

var FollowUpDate = poDueDate.AddDays(-3);

Change the variable to DateTime poDueDate = ttpo.DueDate; ;

Thank you
Now I get this error
“Cannot implicitly convert type ‘System.DateTime?’ to ‘System.DateTime’. An explicit conversion exists (are you missing a cast?)”

Read this documentation on Nullable value types, it should clear things up:

2 Likes

This should work . DateTime FollowUpDate = poDueDate.AddDays(-3);

If I don’t set a var before that I get this error
“The name ‘poDueDate’ does not exist in the current context”

If I set a var before that I get this error

“‘DateTime?’ does not contain a definition for ‘AddDays’ and no accessible extension method ‘AddDays’ accepting a first argument of type ‘DateTime?’ could be found (are you missing a using directive or an assembly reference?)”

Sorry. I mean to fix FollowUpdate .
DateTime poDueDate = ttpo.DueDate;

DateTime FollowUpDate = poDueDate.AddDays(-3);

or DateTime FollowUpDate = ttpo.DueDate.Value.AddDays(-3);

Thank you, the following worked!!

One reason a programmer/designer may use a nullable type because they explicitly want you to handle the null case. Right now your code will throw an exception as you are not handling the case were the date is null.

Thanks for the direction, would you suggest I add

DateTime FollowUpDate = ttpo.DueDate.Value.AddDays(-3);
if(FollowUpDate != null)
{

}

Actually, that wouldn’t prevent the exception since you are already accessing ttpo.DueDate.Value which is the variable that could be null. You want to be checking if ttpo.DueDate has a value before you try adding to it, or then do something else if its null (such as inform the user or pick a reasonable default value)

1 Like

It would have to look something like this…

DateTime FollowUpDate = null;
if (ttpo.DueDate.Value != null)
{
FollowUpDate = ttpo.DueDate.Value.AddDays(-3);
}
else
{
// ttpo.DueDate contains a null value
}

also slightly tangential, ever since we upgraded to 10.2.100 I’ve had to go through our bpms and some customizations and change if (something ! = null) into if (something.Any()) as one by one, they’ve thrown a null reference exception the first time they actually hit a situation where something = null.

Apparently the if evaluates the variable and fails before acting on the fact that it’s null.

Thank you all for the help

@SteveFossey

Would this be another way to do it?

if (!IsNullOrEmpty(String))
{
}

good point, I was thinking about vars.

You could do if (something?.Any() == true). If something is null then null will be compared with true and the if statement will be false.

I usually use the HasValue property on the nullable DateTime because it looks nicer than comparing to null. Both achieve the same result though so it’s really a stylistic choice.

if (ttpo.DueDate.HasValue)

oh good - I come up with objects that don’t have a .any() method. That must be what I need, I’ll test.

is that part possibly redundant?

You need it because whatever arguments you pass to the “if” statement must evaluate to true/false. When something is null then the Any() function isn’t called and therefore null is passed to the “if” statement. The compiler will give you an error so comparing null to true is the workaround for the compiler error.

if (something?.Any() == true)

is just a shortcut for

if(something != null && something.Any()).

It doesn’t save you many keystrokes but in my opinion it makes the code a bit more readable and shows the real intent of the “if” statement: that there must be at least one item in the collection to get into that code block.

1 Like