DateTime for loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everyone,

the below for loop doesn't seem to increment, can anyone please help?

for(DateTime i = DateTime.Now.AddYears(-1); i <= DateTime.Now.AddYears(2);
i.AddYears(1))
{
DateTime dateTime = i;
}

Thanks,

Jon
 
AddYears is a function that *returns* a DateTime.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
the problem is you never increment i

i.AddYears(1) return a value, it does not change i

try

for(DateTime i = DateTime.Now.AddYears(-1); i <= DateTime.Now.AddYears(2);
i= i.AddYears(1))
{
DateTime dateTime = i;
}


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top