This is a simple question I hope. I learned about IIf today and want to implement it in some cases to save a few lines.
IIF(isnumeric(inputs), resume next, call notnum)
This is in red meaning there is a syntax error, how fix this? I have scanned MSDN article on this so I am not being lazy here.
IIf returns a value. As Mat's Mug stated, you can use it, to hand data into another method or function, but you can't call a procedure or function, which has no return value. IsNumeric() for the conditional is okay, though.
That's not how the
IIf
function works.It's a function, not a statement: you use its return value like you would with any other function - using it for control flow is a bad idea.
At a glance,
IIf
works a little bit like the ternary operator does in other languages:Condition, value if true, value if false, and both possible values are of the same type.
It's for turning this:
Into this:
Be careful though,
IIf
evaluates both the true and the false parts, so you will not want to have side-effects there. For example, calling the belowFoo
procedure:Results in this output:
That's why using
IIf
for control flow isn't ideal. But when the true result and false result arguments are constant expressions, if used judiciously, it can help improve your code's readability by turningIf...Else...End If
blocks into a simple function call.Like all good things, best not abuse it.
You cannot use
iif
like this.Here is a possible way to solve this: