tags:

views:

150

answers:

2

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
+8  A: 

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

Michael La Voie
That did the trick. Thanks for your help!
Davey
Sorry .. just to clear something in my head, are you using pre Visual Studio 2008 Davey, I thought inline if was valid from in 2008
spacemonkeys
A: 

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