The Regular Expression
Match object implicitly returns different values in an assignment operation depending on whether or not you use the
The following code snippet illustrates how the same expression on the right side of an assignment operation (e.g.
matches(1)) can return two different types
of values based on the use of the
Dim regEx Set regEx = New RegExp regEx.Pattern = "\d" Dim matches Set matches = regEx.Execute("12345") Dim val1, val2 ' This statement will assign the 'Match' object Set val1 = matches(1) ' This statement will assign the string from the 'Match.Value' property ' and will cause this rule to be raised. val2 = matches(1)
Since many people forget to use the
Set keyword in object-based assignment operations, this type of behavior from the
Match object can lead to
run-time errors where the wrong type of value is assigned to a variable.
The solution to this situation is to always explicitly use the 'Value' property if that is the desired source of your assignment statement. This will remove any ambiguity from your statement and ensure that you are performing the proper type of assignment. Continuing the same example from above, you would replace this statement:
val2 = matches(1)
val2 = matches(1).Value