算术运算导致溢出。

时间:2013-02-12 09:20:55

标签: vb.net

我正在制作一个程序,可以消除十六进制编辑旧DOS游戏(明智的足球世界)的痛苦。

该计划取消了您可以购买的玩家数量限制(5),删除了您可以竞争的最大季节数(20)。

我正在尝试制作它,以便您也可以编辑转移预算,但是当我运行代码选择5000万选项时,我收到Arithmetic operation resulted in an overflow.错误。

以下是我的计划的完整代码(转移限制和最大季节限制都有效):

Imports System.IO

Public Class Swos_Editor_2013

    Dim transferbudget As String

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
    '#########
    '#WARNING#
    '#########
    'Dim answer As DialogResult
    'answer = MessageBox.Show("Make a Backup of your career file then click OK to proceed.  I take no responsibility for damaged career files",
    '                             "*PLEASE READ BEFORE PROCEEDING*", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)


    '##########################
    '#Progress bar set options#
    '##########################
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = 2
    ProgressBar1.Value = 0


    '##################
    '#tool tip options#
    '##################
    Dim toolTip1 As New ToolTip


    toolTip1.AutoPopDelay = 5000
    toolTip1.InitialDelay = 1000
    toolTip1.ReshowDelay = 500
    ToolTip1.ShowAlways = True


    '###################################
    '#Open Swos career file for editing#
    '###################################
    Me.OpenFileDialog1.InitialDirectory = "c:\"
    Me.OpenFileDialog1.Title = "Select Career File for editing"
    Me.OpenFileDialog1.DefaultExt = "*.car"
    Me.OpenFileDialog1.FileName = ""
    Me.OpenFileDialog1.Filter = "Career File(*.car)|*.car"
    Me.OpenFileDialog1.Multiselect = False


    '###################
    '#OK button pressed#
    '###################
    If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then


        '##############################
        '#increase progress bar to 50%#
        '##############################


        If ProgressBar1.Value < ProgressBar1.Maximum Then
            ProgressBar1.Value += 1
        End If


        '################
        '#Enable buttons#
        '################


        btn_save.Enabled = True
        btn_trans_limit.Enabled = True
        btn_career_limit.Enabled = True
        cb_combo.Enabled = True
    End If
End Sub
'#######################
'#Enable combo box drop#
'#######################
Private Sub cb_combo_CheckedChanged(ByVal sender As Object, e As EventArgs) Handles cb_combo.CheckedChanged
    ComboBox1.Enabled = True
End Sub
'########################
'#Select Transfer Budget#
'########################
Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed


    Select Case ComboBox1.SelectedItem
        Case Is = "-£10 million (are you crazy CHALLENGE)"
            transferbudget = "100"
        Case Is = "-£2 million (mid level CHALLENGE)"
            transferbudget = "200"
        Case Is = "-£500k (lower league CHALLENGE)"
            transferbudget = "500"
        Case Is = "£500k (tough)"
            transferbudget = "05"
        Case Is = "£5 million"
            transferbudget = "5"
        Case Is = "£10 million"
            transferbudget = "10"
        Case Is = "£25 million"
            transferbudget = "25"
        Case Is = "£50 million"
            transferbudget = "50000000"
        Case Is = "£99 million"
            transferbudget = "999"                        
    End Select
End Sub
'#######################################
'#exit if EXIT is clicked from dropdown#
'#######################################
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
End Sub
'#######################
'#Not got a bloody clue#
'#######################
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
    target = value
    Return value
End Function
'##################################################################
'#Apply changes made to drop down box AND radio button IF selected#
'##################################################################
Public Sub SaveChanges()
    Dim amount As Integer
    Dim fpath As String = OpenFileDialog1.FileName


    Using stream = New FileStream(fpath, FileMode.Open, FileAccess.ReadWrite)
        Dim result As Integer = 0
        Dim buffer As Byte() = New Byte(3) {}


        stream.Position = 54748
        If (stream.Read(buffer, 0, 4) <> 4) Then
            Throw New Exception(("Didn't read 4 bytes when it should have: " _
                            + (result + "bytes read")))
        End If


        stream.Position = &HD5DC


        buffer = BitConverter.GetBytes(amount)


        stream.Write(buffer, 0, 4)
        stream.Position = &HD880
        If btn_career_limit.Checked Then
            stream.WriteByte(0)
        Else
            stream.WriteByte(1)
        End If


        stream.Position = &HD5C4
        If btn_trans_limit.Checked Then
            stream.WriteByte(&HFF)
        Else
            stream.WriteByte(0)
        End If


        'stream.Write(buffer, 0, 4)
        'stream.Position = &HD5DC
        'If testbutton.Checked Then
        '    stream.WriteByte("3B9AC9FF")
        'End If


        **If cb_combo.Checked = True Then
            stream.Position = &HD5DC
            stream.WriteByte(transferbudget)  ######this is where i get the overflow error#####**
        End If
    End Using
End Sub
'###############################################
'#Save above changes & set progress bar to 100%#
'###############################################


Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
    SaveChanges()


    If ProgressBar1.Value < ProgressBar1.Maximum Then
        ProgressBar1.Value += 1
    End If
End Sub
'#########################################################################################################################################
'#                                                              TOOL TIP (MOUSE HOVER)                                                   #
'#                                                                                                                                       #
Private Sub btn_trans_limit_CheckedChanged(sender As Object, e As EventArgs) Handles btn_trans_limit.MouseHover
    '#
    ToolTip1.SetToolTip(btn_trans_limit, "Removes 5 player transfer limit")                                                             '#
End Sub
Private Sub btn_career_limit_CheckedChanged(sender As Object, e As EventArgs) Handles btn_career_limit.MouseHover                       '#
    '#                                                                                                                                  '#
    ToolTip1.SetToolTip(btn_career_limit, "Removes 20 season limit")                                                                    '#
End Sub
Private Sub ComboBox1_MouseHover(sender As Object, e As EventArgs) Handles ComboBox1.MouseHover                                         '#
    '#                                                                                                                                  '#
    ToolTip1.SetToolTip(ComboBox1, "Select Transfer Budget")                                                                            '#
End Sub
Private Sub btn_save_MouseHover(sender As Object, e As EventArgs) Handles btn_save.MouseHover                                           '#
    '#                                                                                                                                  '#
    ToolTip1.SetToolTip(btn_save, "Save changes to your career file")
End Sub
Private Sub cb_combo_MouseHover(sender As Object, e As EventArgs) Handles cb_combo.MouseHover                                           '#
    '#                                                                                                                                  '#
    ToolTip1.SetToolTip(cb_combo, "tick if you want to edit transfer budget")                                                           '#
End Sub

'#########################################################################################################################################
End Class

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您正在将Stringtransferbudget)传递给FileStream.WriteByte方法。 WriteByte方法需要Byte,而不是String。如果您有Option Strict On,那么这将导致编译错误。但是,由于您有Option Strict Off,因此它允许您编译并自动为您自动插入从StringByte的类型转换。如果字符串中的值超出Byte(0 - 255)的最小值和最大值,则类型转换操作将抛出溢出异常。以下是设置transferbudget变量的代码:

Select Case ComboBox1.SelectedItem
    Case Is = "-£10 million (are you crazy CHALLENGE)"
        transferbudget = "100"
    Case Is = "-£2 million (mid level CHALLENGE)"
        transferbudget = "200"
    Case Is = "-£500k (lower league CHALLENGE)"
        transferbudget = "500"  'GAH!
    Case Is = "£500k (tough)"
        transferbudget = "05"
    Case Is = "£5 million"
        transferbudget = "5"
    Case Is = "£10 million"
        transferbudget = "10"
    Case Is = "£25 million"
        transferbudget = "25"
    Case Is = "£50 million"
        transferbudget = "50000000"  'GAH!
    Case Is = "£99 million"
        transferbudget = "999"  'GAH!            
End Select

如您所见,有几个值大于255。