Re: Why is this simple addition throwing an overflow exception?
- From: "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
- Date: Sun, 26 Nov 2006 03:44:14 GMT
The overflow is occuring when you are attempting to convert the check sum
into a byte. The addition is working, but the total is > 255, which causes
an overflow inside the CByte function. I suspect you really want the
following:
SndCkSum = CByte((SndMin + SndSec + SndT1 + SndT2 + SndPer + SndT1f +
SndT2f) mod 256)
Mike Ober.
"Mike" <nomtrxspam@xxxxxxxxxxx> wrote in message
news:ghbhm2hbj3d3l416b1l6veur33s7rb68pr@xxxxxxxxxx
Hello All,remote basketball
Please, if anyone can point me to the problem, I'd sure appreciate it!
I am very new to VB programming and not a programmer to begin with.
This is part of a Visual Basic 2005 Express Edition program to control a
scoreboard display unit.integer
All I'm trying to do is add 5 byte variables and store the result in an
variable. I added a Try/Catch block to take look at things.of either
This exception occurs only when the clock runs down to 00:00 and when one
SndT1 or SndT2 are some non zero value. The value of 15 for SndMin is dueto the program
automatically starting a halftime clock that starts at 15:00.line of code
CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer <--- The offending
CkSum = 15 + 0 + 99 + 99 + 130 = 343 <-- Values are fromthe texbox in the catch block
The catch block shows CkSum = 200, but 200 is just the value left from thelast addition that worked.
originating were pasted
The variable declrations and the entire sub where the exception is
below from the program code.SndT1 = 99 SndT2 = 99 SndMin = 15
TIA
Mike
This is the output displayed in the textbox by the catch block.
CkSum = 200 SndT1f = 9 HornTime = 1 SndT2f = 9 SndPer = 130
SndSec = 0 ex = System.OverflowException: Arithmetic operation resultedin an overflow.
at ScoreBoard.Form1.SendPacket() in C:\Documents andSettings\Administrator\My Documents\Visual Studio
2005\Projects\ScoreBoard1\ScoreBoard\Form1.vb:line 552assembly
These are are variable declarations involved
Public Class Form1
Inherits System.Windows.Forms.Form
Dim SndMin, SndSec, SndT1, SndT2, SndPer As Byte
Dim SndT1f, SndT2f, SndHrn, SndCkSum As Byte
Dim CkSum as Integer
Dim PacketData() As Byte = {&H55, &HCC, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Public ClockMode As Byte = 0
Public Period As Byte = 1
Public HornTime As Byte = 2
Public T1score As Byte = 0
Public T2score As Byte = 0
Public T1Fouls As Byte = 0
Public T2Fouls As Byte = 0
Public Mins As Byte = 0
Public Secs As Byte = 0
This is the sub in which the overflow exception occurs
'This routine builds a data packet to send to the remote display
HornTime
Private Sub SendPacket()
SndT1 = T1score : SndT2 = T2score : SndPer = Period : SndHrn =
SndMin = Mins : SndSec = Secs : SndT1f = T1Fouls : SndT2f =T2Fouls
If ClockMode = 1 Then ' See if a timeout has been calledbit
SndPer = CByte(SndPer Or &H80) ' Turn on the flash LED flag
End Ifextended display
Try
If ExtDisp = True Then ' Prepare a packet if using an
packet
'Calculate a simple checksum to add to the end of the
True.
The following line will also cause an exception if I change ExtDisp to
+ SndT1f + SndT2f)
SndCkSum = CByte(SndMin + SndSec + SndT1 + SndT2 + SndPer
data packet array
' Load the 7 data bytes and the checksum byte into the
' PacketData(0) and PacketData (1) are initialized to &H55 and &HccPacketData(4) = SndT1
' respectively to form a constant 2 byte header for the packet
PacketData(2) = SndMin : PacketData(3) = SndSec :
PacketData(5) = SndT2 : PacketData(6) = SndPer :PacketData(7) = SndT1f
PacketData(8) = SndT2f : PacketData(9) = SndCkSumpacket
Else ' Prepare a packet for a standard display
'Calculate a simple checksum to add to the end of the data
checksum to a byte
This is the line causing the error.
Line 552
********--> CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer
SndCkSum = CByte(CkSum) ' Explicitly convert the
packet array
' Load the 5 data bytes and the checksum byte into the
' PacketData(0) and PacketData (1) are initialized to &H55 and &HccPacketData(4) = SndT1
' respectively to form a constant 2 byte header for the packet
PacketData(2) = SndMin : PacketData(3) = SndSec :
PacketData(5) = SndT2 : PacketData(6) = SndPer :PacketData(7) = SndCkSum
End IfSndT1f.ToString + " " _
' Send the data packet to the display
Call SendSerialData(PacketData)
' Reset the horn on flag if it was set
If HornTime > &H7F Then HornTime = CByte(HornTime - &H80)
Catch ex As OverflowException
Dim Str As String
TextBox3.Visible = True
Str = "CkSum = " + CkSum.ToString + " SndT1f = " +
+ "HornTime = " + HornTime.ToString + " SndT2f = " +SndT2f.ToString + " " _
+ "SndPer = " + SndPer.ToString + " " _+ " " _
+ "SndT1 = " + SndT1.ToString + " SndT2 = " + SndT2.ToString
+ "SndMin = " + SndMin.ToString + " SndSec = " +SndSec.ToString + _
" ex = " + ex.ToString + " "
TextBox3.Text = Str
Timer1.Stop()
End Try
End Sub
"The scientist is possessed by the sense of universal
causation...His religious feeling takes the form of
rapturous amazement at the harmony of natural law,
which reveals the intelligence of such superiority
that, compared with it, systematic thinking and acting
of human beings is an utterly insignificant reflection."
Albert Einstein (theoretical physicist)
.
- Follow-Ups:
- References:
- Prev by Date: Re: simple format question
- Next by Date: Re: Why is this simple addition throwing an overflow exception?
- Previous by thread: Why is this simple addition throwing an overflow exception?
- Next by thread: Re: Why is this simple addition throwing an overflow exception?
- Index(es):
Relevant Pages
|