RE: DateTime.Value.CompareTo in c#

  • From: "Andy B" <a_borka@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 25 Oct 2007 07:19:09 -0400

Hi Martin...

I have a .net 2.0 Timer object and the if statement is inside its Tick
event. The tick event fires every Timer.Interval (set at 1000) or every 1
second. So the code inside the Tick event runs every second. I tried doing
some code that looks like:

//make a flag saying if we shown the MessageBox yet.
bool ShowMsg=false; //haven't seen the box yet.

//if we haven't seen the box and the times are the same, show the box.
otherwise do nothing...
if(!ShowMsg && DateTime.Now.CompareTo(Time.Value)>0) {
//we will see the box so flag it true
ShowMsg=true;
MessageBox("timer went off");
}

This code got me what I wanted except the MessageBox actually popped up
every Tick (second) regardless of the ShowMsg being true or not. Any ideas?



-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Martin Slack
Sent: Thursday, October 25, 2007 1:44 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: DateTime.Value.CompareTo in c#


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?

  hth

Martin


----- 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>

textBox1.Text=DateTime.Now.CompareTo(Time.Value).ToString();

</test code>






__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: