2016-09-06 41 views
-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 
+0

請張貼的代碼,並在問題異常,並添加說明,當你的代碼不能和你試圖阻止它。 –

+0

切勿將代碼作爲圖像發佈,圖像不可調試。 –

+0

注意#2:在你的問題中,一定要包括你正在做什麼,出了什麼問題和你得到的完整錯誤信息的詳細解釋。 –

回答

0

無論數字型的d變量,這是由你試圖把它遞增到比什麼是可能爲特定類型的較大值造成的。

如果dShort,那麼將有32767最大值。如果它是一個Integer這將有2147483647最大值爲ADD If -statements檢查,如果它要溢出,或使用一個更大的數字數據類型(例如有Long)。

相應的最大值可以通過檢查<data type>.MaxValue領域,例如被發現Integer.MaxValue

+0

你可以修復代碼先生V.V? – Derp

+0

@Derp:不,我不能。我不知道你的變量來自哪裏,它是什麼類型,你使用的是什麼,也不知道它可能包含哪些值。 - 我解釋的很簡單。如果您需要它來保存更大的數字,請更改數據類型。 –

相關問題