Sep
25
2008

Implementing elegant if else

A typical if else condition in VBScript would look like below

If (message = "ok") Then 
    Msgbox "I am calling ok"
Else
    Msgbox "I am calling not ok"
End if

VB provides a elegant function named IIf for such small if else conditions. The function can be easily witten in VBScript

Public Function IIf(ByVal pCondition, ByVal trueValue, ByVal falseValue)
    If pCondition Then
        IIf = trueValue
    Else
        IIf = falseValue
    End If
End Function

Now the same code be written in single line of code

Msgbox "I am calling " & IIf(message="ok","ok", "not ok")
Rating: 6.5/10 (11 votes cast)
Written by Tarun Lalwani in: VBScript | Tags: , , , , , , ,

No Comments

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.