16

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?

Community
  • 1
  • 1
Manny
  • 967
  • 1
  • 6
  • 17
  • This is a duplicate of http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net and YES – PeteT Nov 18 '10 at 15:55

2 Answers2

19

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.
Heinzi
  • 167,459
  • 57
  • 363
  • 519
6

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.

John Hartsock
  • 85,422
  • 23
  • 131
  • 146