89

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

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
  • 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
  • 2
    I 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 Answers11

135

Use IF().

It is a short-circuiting ternary operator.

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

SEE ALSO:

Community
  • 1
  • 1
beach
  • 8,330
  • 3
  • 29
  • 25
  • 6
    Note 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
25

It's actually pretty simple..

If CONDITION Then ..INSERT CODE HERE..
Quintin Robinson
  • 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 : ` 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. – Bart Hofland Apr 04 '19 at 07:40
20

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Fluffy Sebbert
  • 347
  • 2
  • 12
17

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
xpda
  • 15,585
  • 8
  • 51
  • 82
7

Or

IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)
Dmitriy Zhukov
  • 372
  • 2
  • 12
  • 1
    And this is an expression, while the question asked for a statement. ;-) – peSHIr Apr 21 '09 at 06:33
  • 1
    The 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
4

Just add Then:

If A = 1 Then A = 2

or:

If A = 1 Then _
    A = 2
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
3

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-

nora
  • 161
  • 1
  • 8
3
If (X1= 1) Then : Val1= "Yes" : Else : Val1= "Not" : End If
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
S.Ozan
  • 51
  • 6
1

You can use the IIf function too:

CheckIt = IIf(TestMe > 1000, "Large", "Small")
Jon Limjap
  • 94,284
  • 15
  • 101
  • 152
0

Its simple to use in VB.NET code

Basic Syntax IIF(Expression as Boolean,True Part as Object,False Part as Object)As Object

  1. Using IIF same as Ternary
  2. Dim myVariable as string= " "
  3. myVariable = IIf(Condition, True,False)
Rashi
  • 17
  • 1
  • 8
0
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
ravarador
  • 19
  • 1
  • 5