Is it possible to do one line if statement in VB .NET? If so, how?
Use IF().
It is a short-circuiting ternary operator.
Dim Result = IF(expression,<true return>,<false return>)
SEE ALSO:
IIF becomes If, and a true ternary operator
The Ternary Operator in VB.NET
It's actually pretty simple..
If CONDITION Then ..INSERT CODE HERE..
Single line
Syntax:
If (condition) Then (do this)
Example:
If flag = true Then i = 1
Multiple ElseIf's
Syntax:
If (condition) Then : (do this)
ElseIf (condition2) Then : (do this)
Else : (do this)
End If
OR
If (condition) Then : (do this) : ElseIf (condition2) Then : (do this) : Else : (do this) : End If
Multiple operations
Syntax:
If (condition) Then : (do this) : (and this) : End If
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With