# Form Code System.OverflowException • ### Question

• Hello! I am trying to write a code that allows the user to input an initial and final value, click the calculate button, and be given the sum of the odd numbers and sum of the even numbers. When I tried debugging it, the System.OverflowException error popped up over the sum of the odds line. Can anyone tell me what I am doing wrong? Here's my code:

﻿Public Class UserControl1
Private Sub Calculate_Click(sender As Object, ByVal e As System.EventArgs) Handles Calculate.Click
Dim x As Single
Dim y As Single
Dim i As Integer
Dim sumeven As Integer
Dim sumodd As Integer
x = Val(val_iv.Text)
y = Val(val_fv.Text)
i = x
sumeven = 0
sumodd = 0
Do While i <= y
If i Mod 2 = 0 Then
sumeven += i
Else
sumodd += i
End If
Loop
val_even.Text = Str(sumeven)
val_odd.Text = Str(sumodd)
End Sub
End Class

*I bolded the part where the error message highlights the code.

Sunday, April 5, 2020 11:27 PM

### All replies

• Dim x As Single
Dim y As Single
Dim i As Integer
Dim sumeven As Integer
Dim sumodd As Integer
x = Val(val_iv.Text)
y = Val(val_fv.Text)
i = x
sumeven = 0
sumodd = 0
Do While i <= y
If i Mod 2 = 0 Then
sumeven += i
Else
sumodd += i
End If
Loop

One thing that jumps out is that you should not be comparing for equality
between an integer and a float (single).

Another issue is that variable i never changes so you have an infinite loop.

- Wayne

Monday, April 6, 2020 12:01 AM
• Do While i <= y
If i Mod 2 = 0 Then
sumeven += i
Else
sumodd += i
End If
Loop

Try incrementing i:

```Do While i <= y
If i Mod 2 = 0 Then
sumeven += i
Else
sumodd += i
End If
i = i + 1
Loop
```

- Wayne

Monday, April 6, 2020 12:06 AM
• Thank you so much, that was the exact problem!
Monday, April 6, 2020 12:37 AM
• Hi,

Have you solved this problem now?

I think the above reply can provide you with a solution, have you tried it?

If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

Best Regards,

Julie

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Wednesday, April 8, 2020 1:22 AM
• Hello,

Did you get a chance to work with Wayne's recommendation?

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

Thursday, April 9, 2020 1:32 AM