One line if in VB .NET

Viewed 126358

Is it possible to do one line if statement in VB .NET? If so, how?

11 Answers

It's actually pretty simple..

If CONDITION Then ..INSERT CODE HERE..

At the risk of causing some cringing by purests and c# programmers, you can use multiple statements and else in a one-line if statement in VB. In this example, y ends up 3 and not 7.

i = 1
If i = 1 Then x = 3 : y = 3 Else x = 7 : y = 7

Or

IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)

Just add Then:

If A = 1 Then A = 2

or:

If A = 1 Then _
    A = 2

You can use the IIf function too:

CheckIt = IIf(TestMe > 1000, "Large", "Small")
If (condition, condition_is_true, condition_is_false)

It will look like this in longer version:

If (condition_is_true) Then 

Else (condition_is_false)

End If
Related