I have this loop, its purpose is to loop through a range of dates and perform some logic to automate adding entries into the database. The issue is that the incrementing portion, date.AddDays(1.0) isn’t working, and is always the same result, causing an infinite loop. Any insight?
for (DateTime date = DateTime.Now; futureDate.CompareTo(date) > 0; date.AddDays(1.0))
{
// logic here
}
DateTime.AddDaysreturns a new instance without modifyingdate. At the moment you’re throwing away this new instance. Instead, you need to do:Also, I’m not sure why you’re calling
CompareTowhen you could use the<operator. I can’t tell whetherCompareTo(date) > 0is correct without thinking about it for a moment, whereas the intent of the<operator is obvious: