0

I have a class that has a dateTime nullable variable called EndDate and when i call that variable , I want to add an extra minute in the time that is coming from the frontEnd, but I can't call the method that adds minutes to the DateTime type

This is what I would like to do, but as I said, I cannot call the AddMinutes method

Calendar calendar = new Calendar();

calendar.EndDate = calendar.EndDate.AddMinutes(1);

But it fails with:

CS1061 'DateTime?' does not contain a definition for 'AddMinutes' and no extension method 'AddMinutes' accepting a first argument of type 'DateTime?' could be found

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
AllPower
  • 175
  • 1
  • 4
  • 16
  • Take a look at the [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8) struct. You can do something like `calendar.EndDate += TimeSpan.FromMinutes(1);` – Jesse Jan 26 '20 at 03:25
  • It would be good if you show more code, so that we can reproduce what you've got going on but I'll just assume for now that the Nullable doesn't have a AddMinutes. – Vulpex Jan 26 '20 at 03:25

1 Answers1

2

As it is nullable type you cannot call AddMinutes method on it. You need to add question mark after nullable type to call method on not null instances.

Calendar calendar = new Calendar();

calendar.EndDate = calendar.EndDate?.AddMinutes(1);
dropoutcoder
  • 2,627
  • 2
  • 14
  • 32