In vbscript can you use two possible conditions in an if statement? (OR)

前端 未结 3 484
长情又很酷
长情又很酷 2021-01-19 17:45

An example would be:

If filter_purchase = 0 Or \"\" Then
SetDocVar \"filter_purchase\", \"0\"
Else
SetDocVar \"filter_purchase\", CStr(filter_purchase)
End I         


        
3条回答
  •  -上瘾入骨i
    2021-01-19 18:33

    If you're just trying to test for an uninitialized variable then your If expression is actually redundant. All variables in VBScript are variants and all variants start out with a default value of 0/False/"". For example:

    Dim v
    If v = ""    Then MsgBox "Empty string"
    If v = 0     Then MsgBox "Zero"
    If v = False Then MsgBox "False"
    

    All three of these tests will pass. Note how you can compare a single variable against string, numeric, and boolean literals. Uninitialized variables have no type yet, so these kinds of comparisons are completely fine.

    However, once you assign a value to the variable, you need to consider its type when making comparisons. For example:

    Dim v
    v = ""
    If v = ""    Then MsgBox "Empty String"    ' Pass.  "" = "".
    If v = 0     Then MsgBox "Zero"            ' Fail! Illegal comparison.
    If v = False Then MsgBox "False"           ' Fail! "" <> False.
    

    Now that the variant has been defined as holding a string, it will need to be compared against other string types (literals or variables) or values that can be cast (either implicitly or explicitly) to a string.

提交回复
热议问题