Test Design StudioGo to Previous Topic: CA0116 - Missing index argumentGo to Next Topic: CA0119 - Avoid addition operator for string concatenation

CA0117 - Avoid implicit use of RegExp Match.Value

Summary

The Regular Expression Match object implicitly returns different values in an assignment operation depending on whether or not you use the Set keyword. 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 Set keyword.

[VBScript] - Copy Code
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.

Solution

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:

[VBScript] - Copy Code
val2 = matches(1)
with the following statement where you use the Value property explicitly.
[VBScript] - Copy Code
val2 = matches(1).Value