how can i make the day grow in order to exit the for?

hello experts,
i dont understant why my for loop is not reaching the end,
what am i missing?

thank for help.

attached a test method.
[Test]
        public void LoopingWithDays()
        {
            int aYear = 2009;
            int aMonth = 4;
            int aDay = 1;
            DateTime firstDay = new DateTime(aYear, aMonth, 1);
            int nbDays = DateTime.DaysInMonth(aYear, aMonth);
            DateTime today = new DateTime(aYear, aMonth, aDay);
            int i = 0;
            for(DateTime day = firstDay; day.Day <= nbDays; day.AddDays(1))
            {
                if (i > 100) break;
 
                Console.WriteLine("day :" + day.Date);
                i++;
            }
            
        }

Open in new window

Erwin PombettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag AgarwalPython DeveloperCommented:
Actually day.AddDays(1) will return a modified date, not going to change in the calling var instance. So modify your code like this

int aYear = 2009;
            int aMonth = 4;
            int aDay = 1;
            DateTime firstDay = new DateTime(aYear, aMonth, 1);
            int nbDays = DateTime.DaysInMonth(aYear, aMonth);
            DateTime today = new DateTime(aYear, aMonth, aDay);
            int i = 0;
            for (DateTime day = firstDay; day.Day <= nbDays;)
            {
                if (i > 100) break;

                Console.WriteLine("day :" + day.Date);
                i++;
                day = day.AddDays(1);
            }

It should break, i mean the for loop :-)

Anurag
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oobaylyCommented:
DateTime.AddDays doesn't alter the structure it is called upon, but returns a new DateTime structure.
Try this
for(DateTime day = firstDay; day.Day <= nbDays; day = day.AddDays(1)){
  // ...
}

Open in new window

0
Fernando SotoRetiredCommented:

Change this line of code from this:
for (DateTime day = firstDay; day.Day <= nbDays; day.AddDays(1))
 
to this:
 
for (DateTime day = firstDay; day.Day <= nbDays; day = day.AddDays(1))

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.