|
-
Apr 8th, 2001, 11:03 PM
#1
Thread Starter
Junior Member
Hi, I'm writing an application in which I need to log the start and end times of an event and the subsequent difference in time time measured in seconds. I have used text boxes for the times (1 for hour, 1 for min etc). Then when all is completed, I check all the boxes and work out the times.
Ideally I just take the start units from the end, and if the result is less than zero, ie starts at 23:58:01 and ends at 00:12:34 then I effectively add 24hours to get the number. However, this doesn't even come into question because unless there is only a few mins difference and the start hour is less than the end, then I get overflow errors.
**
My assumption was that "integer" was < 32k (two bytes) and that "long" was <2b (four bytes).
If this is the case, why do I get overflow errors when a "long" variable is assigned to 86k ??
If long is not 4 bytes, how do deal with decently large numbers ? Does VB have an unsigned long or some other way of manipulating large numbers.
Code Extract:
lngSum = ((CLng(txtFH) - CLng(txtSH)) * 3600) + ((CLng(txtFM) - CLng(txtSM)) * 60) + (CLng(txtFS) - CLng(txtSS))
lngDay = 24 * 3600
If lngSum < 0 Then
lngTotal = lngDay + lngSum
Else
lngTotal = lngSum
End If
MsgBox lngTotal 'Only here as a debug
Thanks
-
Apr 8th, 2001, 11:11 PM
#2
perhaps Double would do it...?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 8th, 2001, 11:30 PM
#3
Addicted Member
VB seems to get a bit confused about things like this.
You'll notice that if instead of lngDay = 24 * 3600, you do lngDay = 86400 or lngDay = Clng(24) * Clng(3600) you won't get errors.
[From Microsoft Help. Explanation of error]
You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example:
Dim x As Long
x = 2000 * 365 ' Error: Overflow
To work around this situation, type the number, like this:
Dim x As Long
x = CLng(2000) * 365
[End of Microsoft Help]
I think this basically means that VB does the multiplication and tries to store the result in an integer as both 2000 & 365 are integers. The conversion to a Long is not applied until after the multiplication has occurred. That's why setting one of the numbers to a Long will make VB store it in a long and hence remove your problems.
Something like that anyway. 
Hope this helps,
Nathan Liebke
-
Apr 8th, 2001, 11:38 PM
#4
Thread Starter
Junior Member
Solved !
Thanks muchly, didn't think of that.
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
|