Library code snippets

Create an IIF statement in VBScript

Many ASP developers are also Visual Basic developers, and as such, we often have to shift gears between the two, remembering (or trying to remember, at least) which features exist in one and not the other. Differences in error trapping and class definitions are among the more well-recognized, but there are many more subtle discrepancies. For example, VB has a handy function, IIf, which allows you to collapse several lines of Boolean logic into a single expression, like this:

blnFoo1 = True
blnFoo2 = False
MsgBox IIf(blnFoo1 = blnFoo2, "The same!", "Different!")

As you can see, the first parameter represents the expression to evaluate. The second parameter is the result to return if the expression evaluates to True, while the third parameter is the result to return if the expression evaluates to False. VBScript, unfortunately, doesn't support this function, but you can easily write your own equivalent, like this:

Public Function IIf(blnExpression, vTrueResult, vFalseResult)
  If blnExpression Then
    IIf = vTrueResult
  Else
    IIf = vFalseResult
  End If
End Function

Comments

  1. 11 Jun 2004 at 07:57

    the iif (along with tons of others such as left and right) are still available, they are just hidden


    microsoft.visualbasic has all of those goodies that we've come to know and love.


    enjoy

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Create an IIF statement in VBScript.

Leave a comment

Sign in or Join us (it's free).

ElementK Journals

We'd love to hear what you think! Submit ideas or give us feedback