To read the article online, visit http://www.4GuysFromRolla.com/webtech/tips/t041301-1.shtml

User Tips: Creating an IIF Function for VBScript


By Brian O.

I recently noticed that there was no IIf function in VBScript, meaning I can't use IIf when creating ASP pages. When working with VB I've sometimes found the IIf function quite handy to use. For those who don't know, the IIf function takes three parameters:

String = IIf(BooleanExpression, TrueString, FalseString)

The semantics of the IIf function are as follows: if BooleanExpression is True, the IIf function returns the TrueString, else the IIf function returns the False string. This function is handy for a number of common ASP tasks. For example, if you are updating a URL database field and you want to insert "None" if the user doesn't submit a URL through a form, you could use the IIf function like so:

objRS("URL") = iif(Len(Request.Form("URL")) > 0, Request.Form("URL"), "None")

Since VBScript doesn't contain an IIf function, I decided to write my own. You will need to include this function in any ASP pages that you need to use the IIf function (or you can use a server-side include to import this function into all of the ASP pages that need to utilize it).

function iif(psdStr, trueStr, falseStr)
  if psdStr then
    iif = trueStr
  else 
    iif = falseStr
  end if
end function

Pretty simple, eh? Hope this is of use to somebody :)

Happy Programming!

Return to user tips...


Article Information
Article Title: User Tips: Creating an IIF Function for VBScript
Article Author: Brian O.
Article URL: http://www.4GuysFromRolla.com/webtech/tips/t041301-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers