Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a version of the shorthand If-Then-Else in C# (cond ? a : b), in VB.Net? [duplicate]

Tags:

c#

vb.net

Possible Duplicate:
Is there a conditional ternary operator in VB.NET?

Is there a version of the shorthand If-Then-Else in C#:

c = (a > b) ? a : b;

meaning...

if (a > b) {
  c = a; }
else {
  c = b; }

.. in VB.Net?

like image 564
Manny Avatar asked Nov 18 '10 15:11

Manny


2 Answers

You want to use the If operator:

Dim maximum = If(a > b, a, b)

There's also the older Iif function, which still works, but If is superior, since it:

  • performs type inference (if a and b are both integers, the return value will be an integer instead of an object) and
  • short-cuts the operation (if a > b, only a is evaluated, and vice-versa) -- this is relevant if a or b is a function call.
like image 128
Heinzi Avatar answered Oct 16 '22 10:10

Heinzi


Yes the IF is what you want

Here is some reference

http://msdn.microsoft.com/en-us/library/bb513985

Here is its use

c = IF(a > b, a , b)

Obviously there was a operator called IIF but it has been deprecated.

like image 38
John Hartsock Avatar answered Oct 16 '22 12:10

John Hartsock