Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

VB Equivalent to the Java Ternary Operator

In VB9, the equivalent of the Java ternary operator is the new If operator.

Prior to VB9, the equivalent of the Java ternary operator is the If/Else block.  Although the VB IIf function can be used in a similar way to the conditional ternary operator (?) of Java, it is not equivalent.  VB's IIf function needs to evaluate all arguments since it is a method (method calls always evaluate all arguments), but the ternary operator is able to bypass some evaluations (using short-circuit logic in a similar way to the logical operators && and ||).

For example, the following Java code:
Target = Condition ? ResultOne : ResultTwo;

Has the following VB equivalent, prior to VB9:
If Condition Then
    Target = ResultOne
    Target = ResultTwo
End If

The following VB code is not functionally equivalent since both ResultOne and ResultTwo will be evaluated:
Target = IIf(Condition, ResultOne, ResultTwo)

If you need to convert from Java to VB and you are depending on the results being reliable and accurate, then you will want to have Java to VB Converter, the best Java to VB converter, at your fingertips.

Copyright © 1997 - 2017 Tangible Software Solutions Inc.