Is it possible to do one line if statement in VB .NET? If so, how?
-
Be careful with the IIf opertor though - it is not always [short-circuited](http://en.wikipedia.org/wiki/Short-circuit_evaluation) and both the true and false expressions are evaluated. – Paul Alexander Apr 21 '09 at 07:44
-
2I actually think IIF is *never* short-circuited – Brian J Jan 16 '14 at 18:58
-
What's the advantage - or even the purpose - of IFF over If? – Lou Jul 03 '16 at 23:03
-
@LeoKing Backwards compatibility with VB6. Single "I" IF was introduced in VB.NET, whereas Double "I" IIF existed before. And I don't believe any part of VB6 supported short circuit evaluation. – Brian J Aug 30 '17 at 16:05
11 Answers
Use IF().
It is a short-circuiting ternary operator.
Dim Result = IF(expression,<true return>,<false return>)
SEE ALSO:
-
6Note that there can be implicit type casting. For instance: `If(x.HasValue, x.Value, Nothing)` will return 0 if x doesn't contain a value. One way to remedy can be to force value to Nullable like so: `If(x.HasValue, CType( x.Value, Nullable( of Integer) ), Nothing)` – LosManos Jan 25 '13 at 10:49
-
2@LosManos FYI, The reason for the result you got is as follows: If `x` has type `Integer?`, then `x.Value` has type `Integer`, NOT `Integer?`. So the compiler is correct to convert Nothing to `Integer` (to match the other result), resulting in `0`. Just explaining the compiler's behaviour; what you did is a fine solution for what you wanted. An alternative solution, would be to cast the Nothing to the desired type, e.g. `DirectCast(Nothing, Integer?)`. – ToolmakerSteve Mar 19 '14 at 03:55
-
Are you saying that If is checking the type returned by ExpressionIfTrue when it returns ExpressionIfFalse? – Ama Dec 18 '19 at 15:30
It's actually pretty simple..
If CONDITION Then ..INSERT CODE HERE..

- 81,193
- 14
- 123
- 132
-
Yes. Despite I hate this, putting `If...Then...Else...` statements on one line is possible. However, be aware when you combine it with putting multiple statements on one line using colons. The statement `If
Then – Bart Hofland Apr 04 '19 at 07:40: ` will only execute action2 in combination with action 1! Action2 is not a separate statement by itself! So for clarity, I would advise to _always_ use block statements and avoid colons altogether.
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

- 18,379
- 16
- 47
- 61

- 347
- 2
- 12
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

- 15,585
- 8
- 51
- 82
Or
IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)

- 372
- 2
- 12
-
1And this is an expression, while the question asked for a statement. ;-) – peSHIr Apr 21 '09 at 06:33
-
1The downside to `IIf` vs the identical syntax with `If` is that `IIf` is more prone to [run-time errors](http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.90%29.aspx). – Chiramisu Jan 07 '15 at 21:55
One Line 'If Statement'
Easier than you think, noticed no-one has put what I've got yet, so I'll throw in my 2-cents.
In my testing you don't need the continuation? semi-colon
, you can do without, also you can do it without the End If
.
<C#> = Condition.
<R#> = True Return.
<E> = Else Return.
Single Condition
If <C1> Then <R1> Else <E>
Multiple Conditions
If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
Infinite? Conditions
If <C1> Then <R1> Else If <C2> Then <R2> If <C3> Then <R3> If <C4> Then <R4> Else...
' Just keep adding "If <C> Then <R> Else" to get more
-Not really sure how to format this to make it more readable, so if someone could offer a edit, please do-

- 161
- 1
- 8
If (X1= 1) Then : Val1= "Yes" : Else : Val1= "Not" : End If

- 5,753
- 72
- 57
- 129

- 51
- 6
You can use the IIf function too:
CheckIt = IIf(TestMe > 1000, "Large", "Small")

- 94,284
- 15
- 101
- 152
Its simple to use in VB.NET code
Basic Syntax IIF(Expression as Boolean,True Part as Object,False Part as Object)As Object
- Using IIF same as Ternary
- Dim myVariable as string= " "
- myVariable = IIf(Condition, True,False)

- 17
- 1
- 8
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

- 19
- 1
- 5