[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! :afrog:
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?
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
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")
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
Re: Unexpected malfunction when changing from hours to minutes
Quote:
Originally Posted by
Zach_VB6
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?
Re: Unexpected malfunction when changing from hours to minutes
Quote:
Originally Posted by
dbasnett
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