Posted by : Deepak Vasudevan Tuesday, October 07, 2008

The Ternary Operator in VB

Similar to C# and JavaScript, we can actually have the powerful ternary operator in VB also. The ternary operator is an elegant way of replacing multi-line if else elseif statement blocks. For example in JavaScript

if (blnIsTrueFalse)
t = 'true'
else
t = 'false'

can be compressed as

t = (blnIsTrueFalse)?'true':'false'.

Quite interestingly there is a lesser known function in VB called IIF which supports this too. The syntax of the function is

IIF(expression, truepart, falsepart).

Unfortunately, VBScript does not support this feature and hence we may need to simulate the IIF function manually by wrapping the if then elseif block using a custom function.

Popular Post

Blogger templates

Labels

Blog Archive

Followers

Blog Archive

Powered by Blogger.

- Copyright © Lavanya Deepak -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -