Example
result = IIf(2 + 2 = 5, "Correct", "Wrong")
The iif() function is sometimes more convenient than a full-blown if...then...else... control structure. Oddly enough, the function does not exist in VBScript.
Our PHP programmer friends have a superior equivalent, the ternary operator.
Example
$result = (2 + 2 = 5) ? 'Correct' : 'Wrong';
Unfortunately we can't recreate the ternary operator in ASP, but we can recreate the iif() function.
ASP
function IIf(expression, truecondition, falsecondition)
if isNull(expression) then
IIf = falseCondition
elseif cbool(expression) then
IIf = truecondition
else
IIf = falsecondition
end if
end function
2 comments:
Even better if you add an isNull check to the beginning. Your function crashes if expression evaluates to NULL.
if isNull()... elseif cbool()... else....
Thanks Stephen! I've updated the code above.
Post a Comment