One line if in VB .NET

vb.netConditional Operator

vb.net Problem Overview


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

vb.net Solutions


Solution 1 - vb.net

Use IF().

It is a short-circuiting ternary operator.

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

SEE ALSO:

Solution 2 - vb.net

It's actually pretty simple..

If CONDITION Then ..INSERT CODE HERE..

Solution 3 - vb.net

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

Solution 4 - vb.net

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

Solution 5 - vb.net

Or

IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)

Solution 6 - vb.net

Just add Then:

If A = 1 Then A = 2

or:

If A = 1 Then _
    A = 2

Solution 7 - vb.net

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-

Solution 8 - vb.net

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

Solution 9 - vb.net

You can use the IIf function too:

CheckIt = IIf(TestMe > 1000, "Large", "Small")

Solution 10 - vb.net

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)

Solution 11 - vb.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRa&#250;l RoaView Question on Stackoverflow
Solution 1 - vb.netbeachView Answer on Stackoverflow
Solution 2 - vb.netQuintin RobinsonView Answer on Stackoverflow
Solution 3 - vb.netFluffy SebbertView Answer on Stackoverflow
Solution 4 - vb.netxpdaView Answer on Stackoverflow
Solution 5 - vb.netDmitriy ZhukovView Answer on Stackoverflow
Solution 6 - vb.netAnton GogolevView Answer on Stackoverflow
Solution 7 - vb.netnoraView Answer on Stackoverflow
Solution 8 - vb.netS.OzanView Answer on Stackoverflow
Solution 9 - vb.netJon LimjapView Answer on Stackoverflow
Solution 10 - vb.netRashiView Answer on Stackoverflow
Solution 11 - vb.netravaradorView Answer on Stackoverflow