Use the Immediate If Function in VBScript

Use the Immediate If Function in VBScript

Visual Basic includes the Immediate If function (IIf), but VBScript (VBS) does not. However, you can copy this code to VBS to allow the IIf function to be used:

'VB Function not included in VBSFunction IIf(Expression, TruePart, FalsePart)	If Expression = True Then		If IsObject(TruePart) Then			Set IIf = TruePart		Else			IIf = TruePart		End If	Else		If IsObject(FalsePart) Then			Set IIf = FalsePart		Else			IIf = FalsePart		End If	End IfEnd Function

The function can return both objects and basic data types. Heres a sample function from an ASP page that calls the IIf function:

' Return a True or False value for a checkboxFunction CheckBoxValue(Name)	CheckBoxValue = _		IIf(Request.Form(Name) = "on", True, False)End Function
See also  How to Block Websites During Certain Hours on a Mac Computer

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist