2014-11-16 83 views
-1

我無法弄清楚我的代碼有什麼問題。如果你有代碼,代碼應該使用成本,數量和促銷代碼計算物品的總成本。當我放入未使用的字符如!時,它會一直崩潰。任何幫助或改進都會受到歡迎。語法錯誤 - 可計算

Public Class Form1 

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 
    Dim decDisplayTotal As Decimal 
    Dim decPrice As Decimal = txtPrice.Text 
    Dim intQuantity As Integer = txtQuantity.Text 
    Dim strPromoCode As String = txtPromoCode.Text 


    decDisplayTotal = decPrice * intQuantity 
    lblDisplayTotal.Text = "$" & decDisplayTotal 
    If decPrice < 0 Then 
     lblDisplayTotal.Text = ("") 
     txtPrice.Text = Nothing 
     txtQuantity.Text = Nothing 
     txtPromoCode.Text = Nothing 
     MessageBox.Show("Please enter an appropriate price.", "Invalid Input") 
    End If 
    If intQuantity < 0 Then 
     lblDisplayTotal.Text = ("") 
     txtPrice.Text = Nothing 
     txtQuantity.Text = Nothing 
     txtPromoCode.Text = Nothing 
     MessageBox.Show("Please enter an approriate quantity.", "Invalid Input") 
    End If 
    If strPromoCode = ("132") Then 
     MessageBox.Show("You used a limited time, 10% off code! Watch your price drop 10%!", "10% off") 
     decDisplayTotal = 0.9 * (decPrice * intQuantity) 
     lblDisplayTotal.Text = "$" & decDisplayTotal 
    End If 
    If strPromoCode = ("129") Then 
     MessageBox.Show("You used a limited time, 20% off code! Watch your price drop 20%!", "20% off") 
     decDisplayTotal = 0.8 * (decPrice * intQuantity) 
     lblDisplayTotal.Text = "$" & decDisplayTotal 
    End If 
    If strPromoCode = ("136") Then 
     MessageBox.Show("You used a limited time, 30% off code! Watch your price drop 30%!", "30% off") 
     decDisplayTotal = 0.7 * (decPrice * intQuantity) 
     lblDisplayTotal.Text = "$" & decDisplayTotal 
    End If 
    If strPromoCode = ("264") Then 
     MessageBox.Show("You used a limited time, buy 1 get 1 free code, so watch your total cut in half!", "Buy 1 Get 1 Free") 
     decDisplayTotal = 0.5 * (decPrice * intQuantity) 
     lblDisplayTotal.Text = "$" & decDisplayTotal 
    End If 
    If strPromoCode = ("125") Then 
     decDisplayTotal = (decPrice * intQuantity) 
     lblDisplayTotal.Text = "$" & decDisplayTotal 
    End If 
    Try 
     decPrice = Convert.ToInt16(txtPrice.Text) 
    Catch ex As Exception 
     lblDisplayTotal.Text = Nothing 
     MessageBox.Show("Please enter an acceptable price.", "Invalid Input") 
     txtPrice.Text = Nothing 
    End Try 
    Try 
     intQuantity = Convert.ToInt16(txtQuantity.Text) 
    Catch ex As Exception 
     lblDisplayTotal.Text = Nothing 
     MessageBox.Show("Please enter an acceptable quanitity.", "Invalid Input") 
     txtQuantity.Text = Nothing 
    End Try 
    Try 
     strPromoCode = Convert.ToInt16(txtPromoCode.Text) 
    Catch ex As Exception 
     lblDisplayTotal.Text = Nothing 
     MessageBox.Show("Please enter a valid Promo Code.", "Invalid Input") 
     txtPromoCode.Text = Nothing 
    End Try 
End Sub 
Private Sub txtPrice_TextChanged(sender As Object, e As EventArgs) Handles txtPrice.TextChanged 
    lblDisplayTotal.Text = ("") 

End Sub 

Private Sub txtQuantity_TextChanged(sender As Object, e As EventArgs) Handles txtQuantity.TextChanged 
    lblDisplayTotal.Text = ("") 

End Sub 

Private Sub txtPromoCode_TextChanged(sender As Object, e As EventArgs) Handles txtPromoCode.TextChanged 
    lblDisplayTotal.Text = ("") 

End Sub 



End Class 
+1

不能確定你是問這裏。語法錯誤會阻止編譯,但您似乎認爲您正在收到運行時錯誤。 –

回答

1

首先要明白的是,字符串不是數字。 VB允許這種鬆懈,但它以許多微妙的方式回擊。 我認爲你把任何文本框中的單點,然後嘗試使用該文本,因爲它是一個字符串。有時它有時不起作用。

正確的方法是讓框架嘗試轉換,如果它失敗,通知您的用戶這個問題。

所以,而是採用了各種Convert.ToXXXXx使用SomeType.TryParse(IE Int32.TryParse

Dim decPrice As Decimal 
Dim intQuantity As Integer 
Dim strPromoCode As String 

if Not Decimal.TryParse(txtPrice.Text, decPrice) Then 
    MessageBox.Show("Please type a valid number for Price") 
    ClearInputs() 
    return 
End if 

if Not Int32.TryParse(txtPrice.Text, intQuantity) Then 
    MessageBox.Show("Please type a valid number for Quantity") 
    ClearInputs() 
    return 
End if 

Private Sub ClearInputs() 
    lblDisplayTotal.Text = "" 
    txtPrice.Text = "" 
    txtQuantity.Text = "" 
    txtPromoCode.Text = "" 
End Sub 

現在你的類型的值存儲在正確的數據類型的變量,你可以用你的代碼的其餘部分進行.....

.... 
decDisplayTotal = decPrice * intQuantity 
lblDisplayTotal.Text = "$" & decDisplayTotal.ToString 
.... 

您的項目屬性中需要爲項目設置的重要配置是Option Strict On。此配置將禁止字符串和數字之間的隱式轉換,並強制您編寫更正確的代碼。

順便說一下,在strPromoCode的檢查後,您不必再重複這個過程在文本框的字符串轉換爲相應的變量