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.
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.
1 comment:
Thanks
Post a Comment