How to do a single line If statement in VBScript for Classic-ASP?

后端 未结 5 1784
萌比男神i
萌比男神i 2020-12-14 06:41

The \"single line if statement\" exists in C# and VB.NET as in many other programming and script languages in the following format

lunchLocation = (dayOfTheW         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 07:05

    edited 2017/01/28 to adapt to some of the non boolean condition arguments

    Note: If all you need is to select an string based on an boolean value, please, use the code in the Polinominal's answer. It is simpler and faster than the code in this answer.

    For a simple but more "flexible" solution, this code (the original code in this answer) should handle the usual basic scenarios

    Function IIf( Expression, TruePart, FalsePart)
    Dim bExpression
        bExpression = False 
        On Error Resume Next
        bExpression = CBool( Expression )
        On Error Goto 0
        If bExpression 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 If 
    End Function 
    

    If uses the Cbool function to try to convert the passed Expression argument to a boolean, and accepts any type of value in the TrueValue and FalseValue arguments. For general usage this is fast, safe and fully complies to documented VBScript behaviour.

    The only "problem" with this code is that the behaviour of the CBool is not fully "intuitive" for some data types, at least for those of us that constantly change between vbscript and javascript. While numeric values are coherent (a 0 is a False and any other numeric value is a True), non numeric types generate a runtime error (in previous code handled as false), except if it is a string with numeric content or that can be interpreted as true or false value in english or in the OS locale.

    If you need it, a VBScript version "equivalent" to the ? javascript ternary operator is

    Function IIf( Expression, TruePart, FalsePart )
    Dim vType, bExpression 
    
        vType = VarType( Expression )
        Select Case vType
            Case vbBoolean                : bExpression = Expression 
            Case vbString                 : bExpression = Len( Expression ) > 0
            Case vbEmpty, vbNull, vbError : bExpression = False
            Case vbObject                 : bExpression = Not (Expression Is Nothing)
            Case vbDate, vbDataObject     : bExpression = True
            Case Else
                If vType > 8192 Then 
                    bExpression = True
                Else
                    bExpression = False 
                    On Error Resume Next
                    bExpression = CBool( Expression )
                    On Error Goto 0
                End If 
        End Select 
    
        If bExpression 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 If 
    End Function 
    

    BUT independently of the version used, be careful, you are calling a function, not using a ternary operator. Any code, or function call you put in TruePart of FalsePart WILL BE EXECUTED independently of the value of the condition. So this code

    value = IIf( 2 > 3 , DoSomething(), DontDoSomething() )
    

    WILL EXECUTE the two functions. Only the correct value will be returned to value var.

提交回复
热议问题