Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line if in VB .NET

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

like image 697
Raúl Roa Avatar asked Apr 21 '09 06:04

Raúl Roa


4 Answers

Use IF().

It is a short-circuiting ternary operator.

Dim Result = IF(expression,<true return>,<false return>)

SEE ALSO:

like image 101
beach Avatar answered Nov 01 '22 04:11

beach


It's actually pretty simple..

If CONDITION Then ..INSERT CODE HERE..
like image 42
Quintin Robinson Avatar answered Nov 01 '22 04:11

Quintin Robinson


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
like image 19
Fluffy Sebbert Avatar answered Nov 01 '22 02:11

Fluffy Sebbert


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
like image 17
xpda Avatar answered Nov 01 '22 03:11

xpda