Code: Custom IIF for VBscript

I add a custom IIF function to every VBscript I make:

' IIF recreated for VBscript
FUNCTION IIF(Expression, Truepart, Falsepart)
	IF Expression THEN 
		IIF = Truepart
	ELSE
		IIF = Falsepart
	END IF
END FUNCTION

'used like so:
strFlavor = IIF(strColor="brown", "chocolate", "not chocolate")

 

Mind you, it evaluates all parameters on the way in, so even though this checks the objTest object when assigning using it, it would still fail (when the objTest object reference is not set):

strFlavor = IIF(IsObject(objTest), objTest.flavor, "vanilla")

 

It’s no ternary operator, but it’s still indispensible for efficient VBScript coding.

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *