CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2012
    Posts
    10

    How Is This Method Working?!

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How Is This Method Working?!

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

  3. #3
    Join Date
    Sep 2012
    Posts
    10

    Re: How Is This Method Working?!

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How Is This Method Working?!

    Have you tried to do what I've suggested by setting a breakpoint and pressing F5?

    If so, rewriting your code to the following so it makes it easier to see what is going on...

    Code:
    private void timer1_Tick(object sender, EventArgs e)
    {
      var compare = remindTime.CompareTo(DateTime.Now);
    
      if (compare < 0) 
      {
        this.Visible = true;
      }
     }
    Set a breakpoint on the 'if' statement. Press F5. When the breakpoint hits, hover over the 'compare' value. Is it what you expect?

    If it isn't, you are going to have to adjust your code. For example, try using the greater than, rather than less than.
    Try using DateTime.Now.CompareTo(remindTime);

    By stepping through the code as I've suggested, you should be able to inspect the values, see how the code is operating and then figure out how to adjust the code.

  5. #5
    Join Date
    Sep 2012
    Posts
    10

    Re: How Is This Method Working?!

    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!

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How Is This Method Working?!

    Quote Originally Posted by Fanatic2012 View Post
    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...
    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured