Davey
Davey

Reputation: 129

I'm a beginner and don't understand why my code is throwing "Expression Expected"

I am new to development and am receiving the error "Expression Expected" when I attempt to compile the code below. What am I doing wrong?

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

Upvotes: 5

Views: 290

Answers (2)

dbasnett
dbasnett

Reputation: 11773

If you are using 2008? or later the if is ok

    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))

Upvotes: 1

Michael La Voie
Michael La Voie

Reputation: 27926

I'm guessing the exception is being thrown on on the last line

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

You used "if" which is used in an if...then statement, but i bet you meant iif, which is a function

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

just add that one extra "i" and you should be fine

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

Upvotes: 9

Related Questions