我是初学者,不明白为什么我的代码会抛出“表达式预期”

时间:2009-03-13 21:21:06

标签: vb.net

我是开发新手,在尝试编译下面的代码时收到错误“Expression Expected”。我做错了什么?

Public Class Form1

    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click

        Dim Occupation As String = CStr(txtOccupation.Text)
        Dim Bill As Double = CDbl(txtBill.Text)
        Dim Tip As Double = CDbl(txtTip.Text)

        lstOutput.Text = Bill * (1 + if (Tip<1,Tip,Tip/100))

    End Sub
End Class

2 个答案:

答案 0 :(得分:9)

我猜测最后一行正在抛出异常

lstOutput.Text = Bill * (1 + if (Tip<1,Tip,Tip/100))

你在if ... then语句中使用了“if”,但我打赌你的意思是iif,这是一个函数

lstOutput.Text = Bill * (1 + IIf (Tip<1,Tip,Tip/100))

只需添加一个额外的“我”就可以了。

Article explaining the difference between "IF" and "IIF()"

答案 1 :(得分:0)

如果您使用的是2008年?或者以后if是好的

    Dim tipT As New TextBox
    Dim Bill As Decimal = 9D
    Dim tip As Decimal = 20
    tipT.Text = Convert.ToString(Bill * If(tip >= 1, tip / 100, tip))