如何解决此溢出异常未处理/算术运算导致溢出(d = d + 1部分代码)
问题描述:
我在下面的代码中获得OverflowException
。如何解决此溢出异常未处理/算术运算导致溢出(d = d + 1部分代码)
算术运算导致溢出。
Private Sub txtSearch_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
d = 0
c = 0
Dim strTitle As String
Dim b As Short
Dim length As Short
length = Len(txtSearch.Text)
If KeyAscii = 13 Then
rf.MoveFirst()
If txtSearch.Text = "" Then
MsgBox("Search box empty.", MsgBoxStyle.Information)
txtSearch.Focus()
Else
Do Until rf.EOF
strTitle = rf.Fields("Title").Value
For b = 1 To Len(strTitle) - length
If Mid(strTitle, b, length) = txtSearch.Text Then
d = d + 1
Exit For
End If
Next
rf.MoveNext()
Loop
lblTotal.Text = "" & d
If d = 0 Then
MsgBox("Keyword not found.", MsgBoxStyle.Critical)
txtSearch.Text = ""
txtSearch.Focus()
Else
rf.MoveFirst()
Do Until rf.EOF
strTitle = rf.Fields("Title").Value
For b = 1 To Len(strTitle) - length
If Mid(strTitle, b, length) = txtSearch.Text Then
d = d + 1 '<-- Error occurs here.
Exit For
End If
Next
Loop
End If
End If
End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
答
无论数字型的d
变量,这是由你试图把它递增到比什么是可能为特定类型的较大值造成的。
如果d
是Short
,那么将有32767最大值。如果它是一个Integer
这将有2147483647最大值为ADD If
-statements检查,如果它要溢出,或使用一个更大的数字数据类型(例如有Long
)。
相应的最大值可以通过检查<data type>.MaxValue
领域,例如被发现Integer.MaxValue
+0
你可以修复代码先生V.V? – Derp
+0
@Derp:不,我不能。我不知道你的变量来自哪里,它是什么类型,你使用的是什么,也不知道它可能包含哪些值。 - 我解释的很简单。如果您需要它来保存更大的数字,请更改数据类型。 –
请张贴的代码,并在问题异常,并添加说明,当你的代码不能和你试图阻止它。 –
切勿将代码作为图像发布,图像不可调试。 –
注意#2:在你的问题中,一定要包括你正在做什么,出了什么问题和你得到的完整错误信息的详细解释。 –