Hi,
I have encountered an error like the title says and can't really understand what this error means. Could someone explain it?
Exception: "The added or subtracted value results in an un-representable DateTime.\r\nParameter name: months"
Thanks.
What I have tried:
My dates takes only the date, i.e dateTimePicker.Value.Date, so I thought this is the problem that I did not added the .Date everywhere I use the dates, but it didn't helped.
DateTime twoYearsAgo = policy.startDateOfThePolicy.AddYears(-2);
DateTime fiveYearsAgo = policy.startDateOfThePolicy.AddYears(-5);
foreach (var driver in policy.Drivers)
{
if (driver.Claims.Any(claim => claim.ClaimDate.Date >= policy.startDateOfThePolicy.AddYears(-1).Date))
{
policy.Premium = policy.Premium + (policy.Premium + 10);
}
else if(driver.Claims.Any(claim => claim.ClaimDate.Date <= twoYearsAgo.Date && claim.ClaimDate.Date >= fiveYearsAgo.Date))
{
policy.Premium = policy.Premium + (policy.Premium + 10);
}
Policy is a class that holds the premium variable as well as a list of Drivers, policy.Drivers
Driver Class:
<public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
public List<Claim> Claims { get; set; } = new List<Claim>();
Claim class:
public class Claim
{
public DateTime ClaimDate { get; set; }
}
Policy Class:
public class Policy
{
private decimal premium = 100;
public List<Driver> Drivers { get; set; } = new List<Driver>();
public DateTime startDateOfThePolicy { get; set; }
public decimal Premium
{
get { return premium; }
set { premium = value; }
}
}