我在下面的代码中收到OverflowException
。
算术运算导致溢出。
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
答案 0 :(得分:0)
无论d
变量是什么数字类型,都是由于您尝试将其增加到比特定类型可能的值更大的值。
如果d
是Short
,则其最大值为32767.如果是Integer
,则其最大值为2147483647.要么添加{{1} } -statements检查它是否即将溢出,或使用更大的数值数据类型(例如Long
)。
通过查看If
字段可以找到相应的最大值,例如Integer.MaxValue