我无法让我的 If 语句正常工作。
我希望我的 If 语句选中我的两个文本框:名称和价格,以便它们不为空,并选中价格文本框,以便输入的价格/数字等于或大于 1.00。
现在,即使我输入的数字/价格高于 1.00,该程序也会警告我,只有在数字小于 1.00 时才应该这样做。
我正在使用:选项显式打开,选项严格打开。
Private Sub Btn_ResrvCancl_Click(sender As Object, e As EventArgs) Handles Btn_ResrvCancl.Click, listBox_ResDisplay.DoubleClick
If listBox_ResDisplay.SelectedIndex >= 0 Then
If radioBtn_Reserve.Checked Then
If txt_Name.Text Is "" Or txt_Price.Text Is "" Or CDbl(txt_Price.Text) > 1.0 Then
MessageBox.Show("Please enter both your name and price (price must be 1.00 or higher)")
Else
Dim BookingSuccess As Boolean = seatmgrResrv.NewReservation(txt_Name.Text, CDbl(txt_Price.Text), CInt(listBox_ResDisplay.SelectedItem.ToString.Substring(0, 15).Trim))
If BookingSuccess = False Then
MessageBox.Show("Already booked!")
End If
End If
Else
Dim CancelSuccess As Boolean = seatmgrResrv.CancelReservation(CInt(listBox_ResDisplay.SelectedItem.ToString.Substring(0, 15).Trim))
If CancelSuccess = False Then
MessageBox.Show("Already vacant!")
End If
End If
UppsateList()
Else
MessageBox.Show("Please choose a seat")
End If
End Sub
据我了解,这一行一定是不正确的,但我无法找出解决方案:
If txt_Name.Text Is "" Or txt_Price.Text Is "" Or CDbl(txt_Price.Text) > 1.0
提前感谢!
Is
运算符用于引用相等,即两个引用是否引用同一对象,而您希望值相等,为此您使用 =
运算符。 编译器可能会进行优化,以便其中任何一个都可以工作,但这不是不正确的理由。
此外,您不能真正使用 CDbl
因为如果TextBox
为空或包含其他一些非数值,它将引发异常。 如果您要使用 OrElse
而不是Or
那么这将处理空情况,但不会处理任何其他情况。
最后,您希望通知用户金额是否小于 1,而不是大于 1。
总而言之,这就是您的代码应该的样子:
Dim price As Decimal
If txt_Name.Text = String.Empty OrElse
Not Decimal.TryParse(txt_Price.Text, price) OrElse
price < Decimal.One Then
这部分:
CDbl(txt_Price.Text) >= 1.0 Then
将导致您的验证消息在价格大于或等于 1.0 时显示。实际上,您应该检查该值是否严格小于 1.0:
CDbl(txt_Price.Text) < 1.0