我目前正在尝试在if / else语句中查找错误,我进行了一些小测试,以查看变量是否包含值,但这些值都保持不变,但是当我单击“提交”按钮时,该值一直为零。
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
您应在尝试处理输入之前执行输入验证。
使用Option Strict On将确保所有数据类型匹配,例如,它将阻止您尝试将字符串分配给数字变量。
在计算货币值时,最好使用Decimal类型而不是Double。
里面有很多If... ElseIf
s-使用起来看起来会更整洁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
,即不是负数,并且输入的大小也不合理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句