Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a conditional ternary operator in VB.NET?

In Perl (and other languages) a conditional ternary operator can be expressed like this:

my $foo = $bar == $buz ? $cat : $dog; 

Is there a similar operator in VB.NET?

like image 615
Jim Counts Avatar asked Feb 23 '09 03:02

Jim Counts


People also ask

Is ternary operator a conditional operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Is conditional and ternary operator are same?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way.

Can ternary operator have two conditions?

In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on the condition2. If condition2 is correct, then the output is Expression1.

What are the 3 conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.


2 Answers

Depends upon the version. The If operator in VB.NET 2008 is a ternary operator (as well as a null coalescence operator). This was just introduced, prior to 2008 this was not available. Here's some more info: Visual Basic If announcement

Example:

Dim foo as String = If(bar = buz, cat, dog) 

[EDIT]

Prior to 2008 it was IIf, which worked almost identically to the If operator described Above.

Example:

Dim foo as String = IIf(bar = buz, cat, dog) 
like image 92
Beep beep Avatar answered Sep 19 '22 19:09

Beep beep


iif has always been available in VB, even in VB6.

Dim foo as String = iif(bar = buz, cat, dog) 

It is not a true operator, as such, but a function in the Microsoft.VisualBasic namespace.

like image 20
Kris Erickson Avatar answered Sep 20 '22 19:09

Kris Erickson