我寫了一個完美的代碼,只要用戶輸入正確的信息。如果用戶在文本框中輸入數字,我會收到一條錯誤消息,告知用戶必須輸入數據。發生這種情況時,我得到一個轉換異常:無效轉換 - 拋出異常:'System.InvalidCastException'
拋出異常:在Microsoft.VisualBasic.dll中則「」
其他信息:從字符串「」轉換爲類型「整數」是無效的。
我是新來的,我希望有任何建議。
例外發生在這條線:
intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))
如果我刪除嚴格的選項,它會與預期的談話工作,但是該項目要求它的。
Dim intSiteSelected As Double
Dim intNumberOfNights As Integer
Dim blnNightsStayingIsVaild As Boolean = False
Dim blnDiscountIsSelected As Boolean = False
Dim intDiscountChoice As Integer
Dim strDiscountSelected As String = ""
' Call a function to confirm if the Number of Nights staying is valid.
blnNightsStayingIsVaild = ValidateNightsStaying()
' call a function to confirm a discount has been seected.
intDiscountChoice = ValidateSelectedDiscount(blnDiscountIsSelected, strDiscountSelected)
' If number of nights staying and available discount has been selected,
' calculate the subtotal, tax, and final cost.
If (blnNightsStayingIsVaild And blnDiscountIsSelected) Then
intNumberOfNights = Convert.ToInt32(txtNumberOfNights.Text)
intSiteSelected = Me.cmbSelectASite.SelectedIndex
End If
If cmbSelectASite.SelectedIndex = 0 Then
intSiteSelected = 20D
ElseIf cmbSelectASite.SelectedIndex = 1 Then
intSiteSelected = 35D
ElseIf cmbSelectASite.SelectedIndex = 2 Then
intSiteSelected = 55D
End If
intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))
Private Function ValidateNightsStaying() As Boolean
' This function validate the value entered for the number of nights staying in campground.
Dim intNightsStaying As Integer
Dim blnValidityCheck As Boolean = False
Dim StrNumberOfNightsErrorMessage As String =
"Please Enter The Number Of Nights You Are Staying (1-99)"
Dim strMessageBoxTitle As String = " You Must Choose Number Of Nights"
Try
intNightsStaying = Convert.ToInt32(txtNumberOfNights.Text)
If intNightsStaying > 0 And intNightsStaying < 100 Then
blnValidityCheck = True
Else
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
blnValidityCheck = True
End If
Catch Exception As FormatException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
Catch execption As OverflowException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
Catch execption As SystemException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
End Try
Return blnValidityCheck
End Function
試試這個http://www.dotnetperls.com/parse-vbnet – Claudius
是什麼'intDetermineSubTotalCost'?錯誤本身是因爲空字符串('「」')不能轉換爲整數。對於所有的用戶輸入使用Integer.TryParse,所以你可以處理,當他們輸入什麼愚蠢 – Plutonix