2017-02-04 88 views
-1

我現在想找到我的if/else語句錯誤我跑了一些小測試,看看我的變量都抱着值,它們只不過是我不斷收到零當我點擊提交按鈕時。如果聲明沒有給出正確答案ASP.NET

Partial Class _Default 
    Inherits System.Web.UI.Page 

Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles Pack_Weight.TextChanged 

End Sub 

Protected Sub Submit_Button_Click(sender As Object, e As EventArgs) Handles Submit_Button.Click 
    Dim PackageW, MemphisRate, SanFranRate As Double 

    'takes user input from the textbox and places it in the variable 
    PackageW = Pack_Weight.Text 

    'if user selects the dropbox and radiobutton list the selected combo should charge a varying bag fee 
    If Location.SelectedValue = "1" & ShippingList.SelectedValue = "1" Then 
     MemphisRate = 2.2 * PackageW 

    ElseIf Location.SelectedValue = "1" & ShippingList.SelectedValue = "2" Then 
     MemphisRate = 2.59 * PackageW 

    ElseIf Location.SelectedValue = "1" & ShippingList.SelectedValue = "3" Then 
     MemphisRate = 2.92 * PackageW 

    End If 
    'outputs the user selected combo to the total amount textbox 
    Total_Cost.Text = MemphisRate 

    If Location.SelectedValue = "2" & ShippingList.SelectedValue = "1" Then 
     SanFranRate = 2.2 * PackageW 

    ElseIf Location.SelectedValue = "2" & ShippingList.SelectedValue = "2" Then 
     SanFranRate = 2.59 * PackageW 

    ElseIf Location.SelectedValue = "2" & ShippingList.SelectedValue = "3" Then 
     SanFranRate = 2.92 * PackageW 

    End If 

    Total_Cost.Text = SanFranRate 

End Sub 
End Class 
+0

哪給你0?在對它們進行任何計算之前,總是將接收到的輸入解析/轉換爲數字類型。 – Thangadurai

+0

這就是當我目前正在測試以試圖弄清楚,因爲現在一切都給零了,我不知道if語句中的哪個部分可以變爲零。 – AH5811

回答

0

你應該嘗試處理輸入之前執行輸入驗證。

使用Option Strict On將確保所有數據類型匹配,例如它會阻止你嘗試將一個字符串分配給一個數字變量。

當你計算貨幣價值,最好是使用十進制類型,而不是雙。

在有有很多的If... ElseIf秒 - 它看起來有點整潔使用Select Case,像這樣:

Option Strict On 
' ... 

Protected Sub Submit_Button_Click(sender As Object, e As EventArgs) Handles Submit_Button.Click 
    Dim packageW As Decimal 

    ' INPUT VALIDATION 
    If Not Decimal.TryParse(Pack_Weight.Text, packageW) Then 
     ' inform user that a number is required 
     Exit Sub 
    End If 

    If Location.SelectedIndex = -1 Then 
     ' inform user that a location must be selected 
     Exit Sub 
    End If 

    If ShippingList.SelectedIndex = -1 Then 
     ' inform user that a shipping type must be selected 
     Exit Sub 
    End If 

    ' CALCULATE SHIPPING COST 
    Dim shippingRate As Decimal = 0D 

    Select Case Location.SelectedItem.Text 
     Case "1" ' Memphis 
      Select Case ShippingList.SelectedItem.Text 
       Case "1" ' Economy 
        shippingRate = 2.2D 
       Case "2" ' Normal 
        shippingRate = 2.59D 
       Case "3" ' Speedy 
        shippingRate = 2.92D 
      End Select 

     Case "2" ' San Francisco 
      Select Case ShippingList.SelectedItem.Text 
       Case "1" 
        shippingRate = 2.2D 
       Case "2" 
        shippingRate = 2.59D 
       Case "3" 
        shippingRate = 2.92D 
      End Select 

    End Select 

    Dim shippingCost As Decimal = Math.Round(packageW * shippingRate, 2) 
    Total_Cost.Text = shippingRate.ToString() 

End Sub 

我發現.SelectedItem.Text.SelectedValue更可靠。

這是我不太清楚什麼對一些變量的好名字是 - 你應該改變他們是有道理的。

這是不是在如果沒有選擇的狀態下是有效的代碼中的註釋很清楚 - 如果是這樣,取出檢查.SelectedIndex = 1。您可能需要確保用戶輸入了有效的packageW,即不是負數,也不是不合理的大數。

0

您錯誤地使用了&。在VB.NET中,&運算符用於字符串連接。您應該使用關鍵字AndAndAlso

+0

謝謝你,但我把所有的&更改爲和,它仍然沒有正確地通過if語句。 – AH5811