Ternary Conditional Operator (?
in VBScript? Sure.
Friday, July 8, 2011 3:04:08 AM
One of the most elegant built-in operations in C, C++, Java, and JavaScript is the Ternary conditional operator, typically expressed as:
...which is equivalent to this 'if-then-else' pseudo-code:
Using VBScript, I've always missed the simplicity offered by Ternary conditional operators. That is, until I started using the built-in Array() function, and determining the appropriate array index based on a logical test. For example:
In this VBScript example, the "Failure" string is the falseExpression, and the "Success" string is the trueExpression. The "(Abs(0 = Err.Number))" is the actual boolTest. Here, the built-in Abs() function must be used, because VBScript "True" is actually -1, which would return an invalid array index. Recall: We need the array indexes to be either 0 or 1 (not -1).
Although, if you need to invoke a function for the result, then use the built-in Eval() function, like so:
In this example, "Success" will be returned to the built-in Eval() function if the Err.Number is 0, which invokes the Function Success() and returns its result. Otherwise, if Err.Number is not 0, then "Failure" will be returned to the built-in Eval() function, which invokes the Function Failure() and returns the result to the variable by the same name.
At the very least, I hope these simple examples help reduce some of your VBScript code, which may be plagued by multiple lines of If-Then-Else-End If conditional blocks.
Enjoy.
variable = (boolTest) ? trueExpression : falseExpression;
...which is equivalent to this 'if-then-else' pseudo-code:
if (boolTest) {
variable = trueExpression
} else {
variable = falseExpression;
}
Using VBScript, I've always missed the simplicity offered by Ternary conditional operators. That is, until I started using the built-in Array() function, and determining the appropriate array index based on a logical test. For example:
Dim result : result = Array("Failure", "Success")(Abs(0 = Err.Number))
In this VBScript example, the "Failure" string is the falseExpression, and the "Success" string is the trueExpression. The "(Abs(0 = Err.Number))" is the actual boolTest. Here, the built-in Abs() function must be used, because VBScript "True" is actually -1, which would return an invalid array index. Recall: We need the array indexes to be either 0 or 1 (not -1).
Although, if you need to invoke a function for the result, then use the built-in Eval() function, like so:
Function Failure()
' Some lines of code here
Failure = "Error (" & Err.Number & "): " & Err.Description
End Function
Function Success()
' Some lines of code here
Success = "Whatever you want it to be"
End Function
Dim result : result = Eval(Array("Failure", "Success")(Abs(0 = Err.Number)))
In this example, "Success" will be returned to the built-in Eval() function if the Err.Number is 0, which invokes the Function Success() and returns its result. Otherwise, if Err.Number is not 0, then "Failure" will be returned to the built-in Eval() function, which invokes the Function Failure() and returns the result to the variable by the same name.
At the very least, I hope these simple examples help reduce some of your VBScript code, which may be plagued by multiple lines of If-Then-Else-End If conditional blocks.
Enjoy.

Barraco Mármol Jerónimojerobarraco # Thursday, July 14, 2011 3:25:36 PM
Lee HarveyLee_Harvey # Thursday, July 14, 2011 4:42:40 PM
Barraco Mármol Jerónimojerobarraco # Friday, July 15, 2011 1:06:36 AM
Omega JuniorOmegaJunior # Wednesday, July 27, 2011 8:39:32 AM
Nonetheless, interesting find, Lee. I'm curious about the booleanTest part. Written in words, you check whether or not an error number equals 0, which returns a boolean (0 = false, -1 is true), which needs Abs() to get a valid array index.
What I'm curious about is how you get to the error number... in a regular ternary booleanTest expression, I'd write the test in place. With a set-up you propose, it looks like the testing needs to be done prior to checking its outcome, like so:
On Error Resume Next Dim myTestOutcome, myResult myTestOutcome = myTestFunction() myResult = Array("failure", "success")(Abs(Err.Number = 0))Am I right?