Results 1 to 7 of 7

Thread: [RESOLVED] Unexpected malfunction when changing from hours to minutes

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Resolved [RESOLVED] Unexpected malfunction when changing from hours to minutes

    Hey guys!

    I have some code that runs on a timer, .Interval = 1000 ms.

    Here's the code:
    Code:
    CanTest = CanTest OrElse (DateTime.Now.Subtract(LastTest).Hours >= 1)
    If CanTest Then
    	If InputBox(Question(Cur), "Question #" & cur.ToString("###,##0")) = Answer(Cur) Then
    		Correct += 1
    	End If
    	Cur += 1
    Else
    	Dim wait As TimeSpan = LastTest.AddHours(1).Subtract(DateTime.Now)
    	Me.Text = "Employee Tester - Waiting " & wait.ToString("mm") & ":" & wait.ToString("ss") 
    End If
    It works fine... It asks the question, waits an hour, counting down the time to next question in Me.Text - like it should.

    However, when I change it to 65 minutes instead of 1 hour, it acts up. The time until next question starts going up instead of down... The only thing I changed is 1 hour to 65 minutes:

    Code:
    CanTest = CanTest OrElse (DateTime.Now.Subtract(LastTest).Minutes >= 65)
    If CanTest Then
    	If InputBox(Question(Cur), "Question #" & cur.ToString("###,##0")) = Answer(Cur) Then
    		Correct += 1
    	End If
    	Cur += 1
    Else
    	Dim wait As TimeSpan = LastTest.AddMinutes(65).Subtract(DateTime.Now)
    	Me.Text = "Employee Tester - Waiting " & wait.ToString("mm") & ":" & wait.ToString("ss") 
    End If
    Really confused... Please help!

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Unexpected malfunction when changing from hours to minutes

    What happens if you change all the references to minutes and set the number of minutes to 60?

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Unexpected malfunction when changing from hours to minutes

    The result of DateTime.Now.Subtract(LastTest) is a TimeSpan. Assuming you want the total time difference you should be using .Totalsomething.

    This shows what I am talking about

    Code:
            Dim dd As TimeSpan = DateTime.Now.Subtract(LastTest)
    
            Dim hrsD As Double = DateTime.Now.Subtract(LastTest).TotalHours
            Dim minsD As Double = DateTime.Now.Subtract(LastTest).TotalMinutes
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Unexpected malfunction when changing from hours to minutes

    Found the problem - it wasn't with my DateTime arithmetic. When I first called the Timer's .Start(), I never set CanTest = True.

    It works with 65 minutes, changing my .Text to:
    Code:
    Me.Text = "Employee Tester - Waiting " & wait.TotalMinutes.ToString("00") & ":" & wait.ToString("ss")

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Unexpected malfunction when changing from hours to minutes

    Sometimes doing it the long way can be helpful

    Code:
            Dim CanTest As Boolean = False
            Dim LastTest As DateTime = DateTime.Now.AddMinutes(-65) 'set up for a test
            Dim DiffInDate As TimeSpan = DateTime.Now.Subtract(LastTest)
    
            If CanTest OrElse DiffInDate.TotalMinutes >= 65 Then
                Stop
            Else
                Stop
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Unexpected malfunction when changing from hours to minutes

    Quote Originally Posted by Zach_VB6 View Post
    Found the problem - it wasn't with my DateTime arithmetic. When I first called the Timer's .Start(), I never set CanTest = True.

    It works with 65 minutes, changing my .Text to:
    Code:
    Me.Text = "Employee Tester - Waiting " & wait.TotalMinutes.ToString("00") & ":" & wait.ToString("ss")
    Unless you change your check to total minutes the only way the If will ever be true is if CanTest = true. What is this code trying to do?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Unexpected malfunction when changing from hours to minutes

    Quote Originally Posted by dbasnett View Post
    Unless you change your check to total minutes the only way the If will ever be true is if CanTest = true. What is this code trying to do?
    You're right , changed it to ToatlMinutes --- thanks

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