I am using the compare() method to check a saved date and time in a variable by the user VS the system's current time, "Now". The compare() method return -1 if the first date and time is less than the current time, or 0 if the saved date and time is equal to the current time. How is it working (making the application visible when the times are equal) if it's returning -1 from what I can see in the code.
private void timer1_Tick(object sender, EventArgs e)
if (remindTime.CompareTo(DateTime.Now) < 0) //when the dates are equal it should return 0 so this.Visible = true; will not run, but it does.
this.Visible = true;
Thank you all in advance!
Last edited by Fanatic2012; September 7th, 2012 at 02:02 PM.
put a breakpoint on the this.Visible = true; statement. Press F5. Does the breakpoint get hit at the right time?
If not, is the remindTime value what you expect? Is your logic what you expect? According to what you have written, if the remindTime is less than the current time, visible gets set to true. Is that what you want? Should the comparison operator be reversed (i.e. use a > instead of a <)?
I want the user to select the time they want to be reminded by showing the this.Visible = true. Any time inside the remindTime would be less that the current time so it would fire before the Now time, yet it doesn't, it fires when the current "Now" time equals the remindTime. That is what I do not understand.
Arjay I really appreciate your replies. Here is the thing the code IS working as it should, I just do not understand it... The timer checks the user-set reminder date and compares it to the system date and time and runs when the times are equal. Shouldnt the result of the comparison between the Compare() method and the remindTime be equal for it to run? If so how is it working if the result of the comparison is not 0==0 (meaning they are both the same) but instead -1 < 0 (meaning the time is earlier) ? This is not my code I'm trying to figure it out. Please help before my brain blows up!
Shouldnt the result of the comparison between the Compare() method and the remindTime be equal for it to run?
Maybe, but that's not the way it's coded.
Say, have you tried modifying the code and setting a breakpoint as I've suggested (twice).
You can consider and think about code all you want, but looking at what happens in a debugger should show you what's going on very quickly.
I've suggested that you modify the code to...
var compare = remindTime.CompareTo(DateTime.Now);
if (compare < 0)
this.Visible = true;
This code is functionaly identical to the original code, but it will allow you to see what's going on in the debugger.
The behavior of the code is all based on how the if (compare < 0) statement is written, right? If you want different behavior such as 0==0, then you need to change the if statement. As written, the if block gets executed whenever the compare value is less than 0. You can see this immediately if you step through the code in a debugger.
That obviousness being said, you might ask why the developer didn't code it to compare some exactly point in time (i.e. 0==0)? The most likely reason for this is because there is a very high likelyhood that the exact time would be missed based on the frequency of the timer. Time comparisons using CompareTo are going to be down to the milli or micro-second, so usually comparisons like these are done with a > or < operator. Maybe that is the answer you are looking for?
At any rate, from a person that is trying to help, it would be helpful if you mentioned that you tried what was suggested (or didn't try it) in your replies. If I know if you've tried something, I can have an idea what direction I need to go if you post back.