Click here to Skip to main content
14,367,549 members
Rate this:
Please Sign up or sign in to vote.
DateTime d = DateTime.Now.AddDays(10);
            a = d.ToShortDateString().ToString();

public void dateCompare()
       {
           if (a == DateTime.Now.ToShortDateString())
           {               
                   TrayIcon.ShowBalloonTip(500);              
           }
           else
           {
               dateCompare();
           }
       }
Posted
Comments
Wes Aday 6-Apr-14 10:48am
   
And so? What is your question? Why do you have dateCompare endlessly calling itself when you do not change the condition? Learning how to use a debugger is a very valuable skill.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Um...well yes, it would...
For the next ten days, that code will continually call itself, adding to the stack usage each time. Since your processor can probably call itself a couple of hundred thousand times every second without breaking into a sweat, the stack is going to be exhausted very, very quickly...
Why are you calling the method from within the method? It's not even usefully recursive, since if you do meet the condition, it will return from all of the calls immediately, since there is no code below the call to dateCompare except the end of the method...
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

In almost all cases, when you see this exception, this is an indication of having "infinite" recursion:
http://en.wikipedia.org/wiki/Recursion[^],
http://en.wikipedia.org/wiki/Recursion_(computer_science)[^].

Recursion in your code is apparent: this is calling dataCompare from its implementation. By why you never exit this recursion? Because a == DateTime.Now.ToShortDateString() always return false, so you call dataCompare over and over. You simply don't modify a. But this is not enough. The whole idea is wrong: 1) you work not with time, but with strings representing time, you need to work with System.DateTime instead; 2) avoid '==' comparison with time; use '>', '<', '<=' or '<=' instead (do I even need to explain why? just try to think about it).

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100