VBScript comparison operator
VBScript comparison operators are a type of operator that allows you to compare two values and get a Boolean result.
Conditional statements such as If
s are often where the result of comparison operators are passed.
Contents
Equals operator: =
The =
operator can be used between two values, variables, and/or function calls to return whether the two values are equal.
If Var1 = 1 Then Call DoSomething() End If
The above will call the function if Var1
equals one.
Types
VBScript will attempt to convert values of different types in order to compare them. Integer value 1 will be "implicitly" converted to a string to compare to the string value "1", which succeeds. However, it is not a good idea to rely on this.
If "1" = 1 Then AddQ "This comparison will succeed." End If
If you have two string values and compare them using =
, a case-sensitive comparison will be made ("a" does not eqaul "A"). To ignore case, you have two options.
- Use the LCase() or UCase() function on both operands to "level the playing field".
- Use the StrComp() function with CompareMode set to
vbTextCompare
. This method is preferred but often underused.
If LCase(Str1) = LCase(Str2) Then '... End If
If StrComp(Str1, Str2, vbTextCompare) = 0 Then '... End If
Multiple operations
In a statement, you can and often will use the equals operator with other operators, following the order of operations.
If Var1 = 1 + 1 Then AddQ "Var1 equals 2." End If
The 1 + 1
will be computed first, followed by the check whether it is equal to Var1
.
Even the Assignment operator can be combined with the equals operator (however confusing this can get):
BoolVar = Var1 = 1
In the above, VBScript will determine that the first =
is for assignment and the second is for comparison. It will take whether Var1 = 1
and store it as a Boolean in BoolVar
.
Objects
Objects cannot be compared using the =
operator. The result will be vague errors like "object variable not set". Use the Is
operator instead.
Greater than and less than operators: >
, <
The greater than operator, >
, and less than operator, <
, allow you to compare numbers to see if one is (obviously) greater than or less than the other.
If Var1 > 1 Then AddQ "Var1 is greater than 1." ElseIf Var1 < 1 Then AddQ "Var1 is less than 1." End If
These operators work if both values are numeric, both values are strings, or both values are dates. A "type mismatch" error is raised for other type combinations.
Combining: >=
, <=
You can combine the equals (=
) and greater than (>
) operator to make the greater than or equal to operator: >=
. You can combine the equals (=
) and less than (<
) operator to make the less than or equal to operator: <=
.
If Var1 >= 1 Then AddQ "Var1 is greater than or equal to 1." ElseIf Var1 <= 2 Then AddQ "Var1 is less than or equal to 2." End If
Note that =>
and =<
work as well, but are not commonly used.
Not equal to operator: <>
The opposite of the equals (=
) operator is the not equal to operator: <>
. This operator behaves like the equals operator but returns True when the two operands are different. Types are implicitly converted.
If Var1 <> 1 Then AddQ "Var1 does not equal 1." End If
This operator obviously came from the combination of <
and >
, but it functions more like the equals operator than the greater than or less than operators. ><
works the same, but is not commonly used.
Objects cannot be compared, instead use the Not
and the Is
operators.
Is
operator: compare object references
The Is
operator allows you to compare two values of type Object. Any other types compared using Is
will result in an error.
When you use the Set
keyword to assign an object to a variable, you are only assigning a "reference" to that object (all other types in VBScript are assigned by value).
Set Obj = CreateObject("Scripting.Dictionary") Set Obj2 = Obj
In this example, both variables reference the same Scripting.Dictionary. To determine whether they are referencing the same object, use the Is
keyword.
If Obj Is Obj2 Then AddQ "Both variables reference the same object." End If
In contrast, two objects are created if we do this:
Set Obj = CreateObject("Scripting.Dictionary") Set Obj2 = CreateObject("Scripting.Dictionary")
To check if an object "is not" another object, we have to check if the opposite is true with the logical Not
operator:
If Not Obj Is Obj2 Then AddQ "The variables reference different objects." End If
Nothing
As mentioned before, objects should be cleaned up by setting the variables referencing them to Nothing
. Anything set to Nothing
will be "equal" to Nothing
:
Set Obj = Nothing If Obj Is Nothing Then AddQ "The variable references Nothing." End If
Logical operators
Template:Main Logical operators allow you to compute Boolean values from other Boolean values in order to make "complex" comparisons.