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

不羁的心 提交于 2019-12-31 02:24:11

问题


An example would be:

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

But I get a 'Type Mismatch'. Would there be an easier way than doing Else IFs?


回答1:


you have to explicitly state the condition for each OR. Please see below

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



回答2:


This should be the condition you want

If ((filter_purchase = 0) Or (filter_purchase = "")) Then

@agamike, I believe a single = is used for comparison in a vbs if not and not == link here




回答3:


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.



来源:https://stackoverflow.com/questions/24165620/in-vbscript-can-you-use-two-possible-conditions-in-an-if-statement-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!