Why is this simple addition throwing an overflow exception?

M

Mike

Hello All,
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 remote basketball
scoreboard display unit.

All I'm trying to do is add 5 byte variables and store the result in an integer
variable. I added a Try/Catch block to take look at things.
This exception occurs only when the clock runs down to 00:00 and when one of either
SndT1 or SndT2 are some non zero value. The value of 15 for SndMin is due to the program
automatically starting a halftime clock that starts at 15:00.

CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer <--- The offending line of code
CkSum = 15 + 0 + 99 + 99 + 130 = 343 <-- Values are from the texbox in the catch block
The catch block shows CkSum = 200, but 200 is just the value left from the last addition that worked.


The variable declrations and the entire sub where the exception is originating were pasted
below from the program code.

TIA
Mike

This is the output displayed in the textbox by the catch block.

CkSum = 200 SndT1f = 9 HornTime = 1 SndT2f = 9 SndPer = 130 SndT1 = 99 SndT2 = 99 SndMin = 15
SndSec = 0 ex = System.OverflowException: Arithmetic operation resulted in an overflow.
at ScoreBoard.Form1.SendPacket() in C:\Documents and Settings\Administrator\My Documents\Visual Studio
2005\Projects\ScoreBoard1\ScoreBoard\Form1.vb:line 552


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 assembly

Private Sub SendPacket()
SndT1 = T1score : SndT2 = T2score : SndPer = Period : SndHrn = HornTime
SndMin = Mins : SndSec = Secs : SndT1f = T1Fouls : SndT2f = T2Fouls
If ClockMode = 1 Then ' See if a timeout has been called
SndPer = CByte(SndPer Or &H80) ' Turn on the flash LED flag bit
End If
Try
If ExtDisp = True Then ' Prepare a packet if using an extended display

'Calculate a simple checksum to add to the end of the packet

The following line will also cause an exception if I change ExtDisp to True.

SndCkSum = CByte(SndMin + SndSec + SndT1 + SndT2 + SndPer + SndT1f + SndT2f)

' Load the 7 data bytes and the checksum byte into the data packet array
' PacketData(0) and PacketData (1) are initialized to &H55 and &Hcc
' respectively to form a constant 2 byte header for the packet

PacketData(2) = SndMin : PacketData(3) = SndSec : PacketData(4) = SndT1
PacketData(5) = SndT2 : PacketData(6) = SndPer : PacketData(7) = SndT1f
PacketData(8) = SndT2f : PacketData(9) = SndCkSum

Else ' Prepare a packet for a standard display

'Calculate a simple checksum to add to the end of the data packet

This is the line causing the error.
Line 552
********--> CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer

SndCkSum = CByte(CkSum) ' Explicitly convert the checksum to a byte

' Load the 5 data bytes and the checksum byte into the packet array
' PacketData(0) and PacketData (1) are initialized to &H55 and &Hcc
' respectively to form a constant 2 byte header for the packet

PacketData(2) = SndMin : PacketData(3) = SndSec : PacketData(4) = SndT1
PacketData(5) = SndT2 : PacketData(6) = SndPer : PacketData(7) = SndCkSum
End If

' 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 = " + SndT1f.ToString + " " _
+ "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)
 
M

Michael D. Ober

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 said:
Hello All,
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 remote basketball
scoreboard display unit.

All I'm trying to do is add 5 byte variables and store the result in an integer
variable. I added a Try/Catch block to take look at things.
This exception occurs only when the clock runs down to 00:00 and when one of either
SndT1 or SndT2 are some non zero value. The value of 15 for SndMin is due to the program
automatically starting a halftime clock that starts at 15:00.

CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer <--- The offending line of code
CkSum = 15 + 0 + 99 + 99 + 130 = 343 <-- Values are from the texbox in the catch block
The catch block shows CkSum = 200, but 200 is just the value left from the last addition that worked.


The variable declrations and the entire sub where the exception is originating were pasted
below from the program code.

TIA
Mike

This is the output displayed in the textbox by the catch block.

CkSum = 200 SndT1f = 9 HornTime = 1 SndT2f = 9 SndPer = 130
SndT1 = 99 SndT2 = 99 SndMin = 15
SndSec = 0 ex = System.OverflowException: Arithmetic operation resulted in an overflow.
at ScoreBoard.Form1.SendPacket() in C:\Documents and
Settings\Administrator\My Documents\Visual Studio
 
S

Stephany Young

Beacuse your 5 variables are of type Byte, the way you are doing the
addition, the interim result will be of type Byte and 343 is too big to fit
in a Byte which has a miximum value of 255.

If interim result was less than 256 then the interim would be converted to
an Integer and assigned to CkSum.

If you don't want to do explicit conversion then you need to do it this way:

CkSum = SndMin
CkSum += SndSec
CkSum += SndT1
CkSum += SndT2
CkSum += SndPer



Because you addition of the 5 Byte variables results in a Bayte value that
is then supp
 
M

Mike

Hello Mike and Stephany,

Thanks for the help! I got it working.
Mike, Unfortunately your solution did exactly the same thing as my original code,
but from your comments I understand now that CByte(x) will overflow if x > 255.

Stephany, Your comment reguarding the interim result of the addition being > 255 was the
other half of the solution.

To test the overflow from the interim results I added the the following 2 lines of code
to the main form load sub and sure enough, it throws an exception pointing to the 2nd
line below.

SndMin = 15 : SndSec = 59 : SndT1 = 99 : SndT2 = 99 : SndPer = 130
CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer

I don't know the accepted way to do such an addition, but out of curiousity
I tried the following which worked fine, no exception during form loading.

SndMin = 15 : SndSec = 59 : SndT1 = 99 : SndT2 = 99 : SndPer = 130
CkSum = 0
CkSum = CkSum + SndMin + SndSec + SndT1 + SndT2 + SndPer

So evidently the interim result can go as high as the largest size variable
used to the right of the equal sign. That's different from the older languages
that I have experience with. They all handled interim results without having
to deal with it.

The next thing I tried was to use the following 2 lines in the SendPacket sub
and it works just fine.

CkSum = SndMin
SndCkSum = CByte((CkSum + SndSec + SndT1 + SndT2 + SndPer) And &HFF)

Lastly I tried Stephanys code as follows and it works also and now I understand why.

CkSum = SndMin
CkSum += SndSec
CkSum += SndT1
CkSum += SndT2
CkSum += SndPer
SndCkSum = CByte(CkSum And &HFF)

Thanks again for the help. Now I can move on and start building the cabnet for the
remote display assembly.

Mike





"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)
 
O

OHM

Just out of curiosity, what are you doing ?



Mike said:
Hello Mike and Stephany,

Thanks for the help! I got it working.
Mike, Unfortunately your solution did exactly the same thing as my
original code,
but from your comments I understand now that CByte(x) will overflow if x >
255.

Stephany, Your comment reguarding the interim result of the addition being
other half of the solution.

To test the overflow from the interim results I added the the following 2
lines of code
to the main form load sub and sure enough, it throws an exception pointing
to the 2nd
line below.

SndMin = 15 : SndSec = 59 : SndT1 = 99 : SndT2 = 99 : SndPer = 130
CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer

I don't know the accepted way to do such an addition, but out of
curiousity
I tried the following which worked fine, no exception during form loading.

SndMin = 15 : SndSec = 59 : SndT1 = 99 : SndT2 = 99 : SndPer = 130
CkSum = 0
CkSum = CkSum + SndMin + SndSec + SndT1 + SndT2 + SndPer

So evidently the interim result can go as high as the largest size
variable
used to the right of the equal sign. That's different from the older
languages
that I have experience with. They all handled interim results without
having
to deal with it.

The next thing I tried was to use the following 2 lines in the SendPacket
sub
and it works just fine.

CkSum = SndMin
SndCkSum = CByte((CkSum + SndSec + SndT1 + SndT2 + SndPer) And &HFF)

Lastly I tried Stephanys code as follows and it works also and now I
understand why.

CkSum = SndMin
CkSum += SndSec
CkSum += SndT1
CkSum += SndT2
CkSum += SndPer
SndCkSum = CByte(CkSum And &HFF)

Thanks again for the help. Now I can move on and start building the cabnet
for the
remote display assembly.

Mike





"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)
 
M

Mike

Just out of curiosity, what are you doing ?

I'm building a basketball scoreboard for my church to use in a youth
basketball league we are starting up.
All the timing and control are done in the vb program that I've been
wrestling with and it will run on a pc at the scorers table. The main
display is battery powered and will receive data from the pc via a 432MHz
rf link so that we can place it wherever we need it. It's all working
well now and if the rf modules are any good at all we should have a really
nice setup.
Thanks again to Mike and Stephany for their help.

Mike



"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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top