Re: DateTime.Value.CompareTo in c#

  • From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 25 Oct 2007 06:43:54 +0100

Hi Andy,

Is your if statement in a loop of some kind? If it is, part of its expression evaluation would have to execute *very* close to the instant when the two times are equal for the expression to evaluate as true. Why not make the comparison >= 0 (greater than or equal to zero) so you are sure to notice exactly when or shortly after the times are equal?



----- Original Message ----- From: "Andy B" <a_borka@xxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, October 25, 2007 12:51 AM
Subject: DateTime.Value.CompareTo in c#

Anybody know why this code doesnt work? Put inside the Tick event of a
timer. The Time.Value is the Value from a DateTimePicker called Time. It is
supposed to show a MessageBox when the time/date on the DateTimePicker
matches the current system time exactly... Any idea how this is supposed to
work? The docs on DateTime.CompareTo(DateTime Date) said that if the result
of CompareTo == 0, then the 2 dates match. It seems broke. I ran a test on
it: Put a textBox on the form and change the MessageBox line to:
textBox1.Text=DateTime.Now.CompareTo(Time.Value).ToString(); Now run the
program. Change the DateTimePicker to a date earlier than the system time,
and the value -1 shows up in the box (expected). Change the picker to a time
later than the system time, and you get 1 in the box (expected). Wait until
the system time catches up with the picker... no 0? It just goes from -1 to
1... *confused*...
<original code>
if (DateTime.Now.CompareTo(Time.Value) == 0)

MessageBox.Show("times up");

</original code>

<test code>


</test code>

View the list's information and change your settings at

Other related posts: