Select Gamer
Select Gamer

Reputation: 11

Visual Basic 2010 Overflow Error

So I'm making a game with an infinitaly expandable length of numbers and I've gone to 2.1 Billion. This gives an overflow error.

Arithmetic operation resulted in an overflow.

In my code I have these two variables:

Dim coal As BigInteger
Dim totalCoal As BigInteger

And these two app settings I save them to:

enter image description here

My.Settings.coal = coal.ToString()
My.Settings.totalCoal = totalCoal.ToString()

I can't post more than 2 links so ill link a .rar file with the images in it. https://dl.dropbox.com/s/s30knl44p9luhjy/Screenshots.rar?dl=0

My main concern is not being able to fix this issue as I have already distributed the application link. I need to know how to make the values unlimited in length of large numbers.

' Import Code
Dim data As New StreamReader("C:\Temp\Data.txt")
Dim value As String = ""
Dim dataArray(1) As String
Dim i As Integer = 0
Dim setValue As Integer = 0
Do Until data.Peek = -1
    'Get 1 Value from Text File at a time
    value = data.ReadLine()
    'Place Value into Array
    dataArray(i) = value
    'Output
    saveValues.Items.Add(dataArray(i))
    'Set Values
    If setValue = 0 Then
        My.Settings.coal = saveValues.Items(0)
        setValue = 1
    ElseIf setValue = 1 Then
        My.Settings.totalCoal = saveValues.Items(1)
        setValue = 2
    End If
    i += 1
    If setValue = 2 Then
        Application.Restart()
    End If
Loop

' Export Code
saveValuesSet.Text =
My.Settings.coal & Environment.NewLine &
My.Settings.totalCoal
Using writer As StreamWriter = New StreamWriter("C:\Temp\Data.txt")
    writer.WriteLine(saveValuesSet.Text)
End Using

Upvotes: 1

Views: 1131

Answers (1)

Blackwood
Blackwood

Reputation: 4534

2 billion is the maximum value for the Integer data type, you can use Long instead to allow you to go up to about 9 * 10^18.

If you want to use numbers that can be of any size at all, you should look at System.Numerics.BigInteger. You will need to add a reference to System.Numerics to your project in order to use BigInteger.

If you want to store a BigInteger in Settings, you could create a Setting with type String and store the string representation there (of course the String could be extremely long).

My.Settings.BigIntegerString = myBigInteger.ToString

Upvotes: 2

Related Questions