Arithmetic operation resulted in an overflow.

Member_2_5230414
Member_2_5230414 used Ask the Experts™
on
I am getting the error Arithmetic operation resulted in an overflow.

Function setthepercentage(ByVal thegoals As List(Of Integer), ByVal thename As String) As String

        Dim over05 As Integer = 0
        Dim over15 As Integer = 0
        Dim over25 As Integer = 0
        Dim countelements As Integer = 0
        'work out if it is 0.5 - 1,3,4,5,3,2,4,5,

        For Each element As String In thegoals
            'chekc if over 0.5
            countelements = countelements + 1
            If element > 0 Then
                over05 = over05 + 1
                'check if over 1.5
            ElseIf element > 1 Then
                over15 = over05 + 1
                'check if over 2.5
            ElseIf element > 2 Then
                over25 = over05 + 1
            End If

        Next

        over05 = (over05 / countelements) * 100
        If over05 <> "NaN" Then
            Session(thename & "over05") = over05
        End If
        over15 = (over15 / countelements) * 100

        If over15 <> "NaN" Then
            Session(thename & "over15") = over15
        End If

        over25 = (over25 / countelements) * 100

        If over25 <> "NaN" Then
            Session(thename & "over25") = over25
        End If
    End Function

Open in new window


What its supposed to do is set the percentage...  

so if elements number if greater then 0 i added 1 to the tally

 If element > 0 Then
   over05 = over05 + 1
end if

Open in new window

I also count how many times it goes through a loop so

        countelements = countelements + 1

Open in new window

this give me the numbers to work with so:

over05 = (over05 / countelements) * 100

Open in new window


but this gives me the error Arithmetic operation resulted in an overflow.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
Use Long instead of Integer as the data type

Author

Commented:
I seem to get the same error
Freelance programmer / Consultant
Commented:
I'd single step through your code (or use a pencil and paper and work through it).  There might be something wrong in the logic.
If it overruns a Long we are talking pretty BIG numbers.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Carl TawnSenior Systems and Integration Developer

Commented:
Can you post samples of some input values that cause the overflow?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Could the problem be with following conditions?

If over05 <> "NaN" Then

Comment these out and see what happens.

If you still get an error, tell us on which line.

Author

Commented:
Walked through it and found i had 0 0/0*0 cant be done :) thanks
Top Expert 2015

Commented:
It can be done, but by putting the result in a Double, which is able to record an infinite.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial