|
-
Dec 29th, 2010, 03:54 PM
#1
Thread Starter
Frenzied Member
[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!
-
Dec 29th, 2010, 04:13 PM
#2
Frenzied Member
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?
-
Dec 29th, 2010, 04:19 PM
#3
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
-
Dec 29th, 2010, 04:24 PM
#4
Thread Starter
Frenzied Member
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")
-
Dec 29th, 2010, 04:27 PM
#5
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
-
Dec 29th, 2010, 04:30 PM
#6
Re: Unexpected malfunction when changing from hours to minutes
 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?
-
Dec 29th, 2010, 04:47 PM
#7
Thread Starter
Frenzied Member
Re: Unexpected malfunction when changing from hours to minutes
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|