I am working on a bandwidth meter and my bytes sent/received overflow long type. I tried currency but all the numbers are negative.
Printable View
I am working on a bandwidth meter and my bytes sent/received overflow long type. I tried currency but all the numbers are negative.
Try Double or Single
Double is 8 byte floating point number which covers the range of -1.79E+308 to 1.79E+308
Single is a 4 byte floating point number which covers the range of -3.40E38 to 3.40E38
Can you show us how you did it?Quote:
Originally Posted by frozen
Single is also 4 byte, and goes to ~ +- 10^38
zaza
EDIT: Rats. Hacked! :)
Well so far double seems to be working.
In a nut shell I have
VB Code:
Private BytesReceived As Double Private BytesSent As Double Private Sub Timer1_Timer() If BytesReceived = 0 And BytesSent = 0 Then BytesReceived = IpHelper.BytesReceived BytesSent = IpHelper.BytesSent Exit Sub End If Download = IpHelper.BytesReceived - BytesReceived Upload = IpHelper.BytesSent - BytesSent End Sub
Can't you just trap the overflow error and then switch to KB, then MB, then GB? Make a global variable to store the mode you are keeping track in, and then divide the bytes by the current mode amount when you update. Something like (a more sophisticated version of) this:Quote:
Originally Posted by frozen
VB Code:
Private lDivisor As Long 'Holds the current "mode" Private lTotal As Long 'Holds the amount of data denominated by the "mode" Public Sub AddBytes(lBytes As Long) On Error GoTo OverFlowCheck If lDivisor Then lBytes = lBytes \ lDivisor lTotal = lTotal + lBytes 'NOTE: this is the only possible overflow in the sub. OverFlowCheck: If Err.Number = 6 Then 'Overflow If lDivisor Then lDivisor = lDivisor * 1024 Else lDivisor = 1024 End If lTotal = (lTotal \ lDivisor) + (lBytes \ lDivisor) End If End Sub Private Function DisplayAs() As String 'Returns a string to append on the end of your display. Dim lTemp As Long lTemp = lDivisor \ 1024 Select Case lTemp Case 0 DisplayAs = "Bytes" Case 1 DisplayAs = "KB" Case 2 DisplayAs = "MB" Case 3 DisplayAs = "GB" Case 4 DisplayAs = "TB" 'If you get here, I want your connection :P End Select End Function