Issue:
You have a procedure or other code that evaluates strings in a case sensitive (or insensitive) manner. You would like an easy way to convert that code so that both case sensitive and insensitive evaluations can be performed easily.
Cause:
When most developers write their code they fail to take into account that future uses of their code may need both case sensitive and insensitive evaluations. This is just poor implementation on the developer’s part, or may be part of a performance optimization. The former is most likely.
Solution:
The first step you need to do is implement a variable to indicate which type of evaluation you want to perform. This variable (or constant) should be of a Boolean data type. The standard that I use is blnCase, where True = Case Sensitive and False = Case Insensitive.
Examples of how to implement the blnCase as a variable or constant
'This method places the blnCase variable in the procedure parameters list
Sub procedure1 (value1, value2, blnCase)
blnStrComp(value1, operator, value2, blnCase)
End Sub
'This method declares a local constant
Function fx1 (value1, value2)
Const blnCase = True
blnStrComp(value1, operator, value2, blnCase)
End Function
'This method relies on a global variable (or constant).
Dim blnCase
blnCase = True
Sub procedure1 (value1, value2)
blnStrComp(value1, operator, value2, blnCase)
End Sub
After you have added the required variable to your procedure/code you can implement the blnStrComp function to easily convert any code to allow both case sensitive and insensitive comparisons. blnStrComp returns a Boolean based on the results of the strings/values being evaluated against the provided operator. All 6 common operations are supported (=, >, <, >=, <=, <>).
Code Examples:
'Current code
If strValue1 = strValue2 Then Wscript.Echo "Equal Strings"
If strValue1 > strValue2 Then Wscript.Echo "strValue1 >"
If strValue1 < strValue2 Then Wscript.Echo "strValue1 <"
If strValue1 >= strValue2 Then Wscript.Echo "strValue1 >="
If strValue1 <= strValue2 Then Wscript.Echo "strValue1 <="
If strValue1 <> strValue2 Then Wscript.Echo "Not Equal Strings"
'New Code with blnStrComop
Const blnCase = True
If blnStrComp(strValue1, "=", strValue2, blnCase) Then Wscript.Echo "Equal Strings"
If blnStrComp(strValue1, ">", strValue2, blnCase) Then Wscript.Echo "strValue1 >"
If blnStrComp(strValue1, "<", strValue2, blnCase) Then Wscript.Echo "strValue1 <"
If blnStrComp(strValue1, ">=", strValue2, blnCase) Then Wscript.Echo "strValue1 >="
If blnStrComp(strValue1, "<=", strValue2, blnCase) Then Wscript.Echo "strValue1 <="
If blnStrComp(strValue1, "<>", strValue2, blnCase) Then Wscript.Echo "Not Equal Strings"
As you can see from the example converting your existing code should be relatively easy because the format of the new comparisons is very similar to the existing comparisons. While you can convert all existing comparisons to the blnStrComp procedures, all that may be required to implement this solution is to convert only the comparisons that directly affect the Case Sensitive and Case Insensitive comparisons.
For example if you are sorting data you only need to modify the comparisons that are meaningful in the sort procedure. You may not need to modify the comparisons outside the sort procedure.
blnStrComp lends itself very well to easily converting any Sort or Search procedure to allow both Case Sensitive and Case Insensitive comparisons.
blnStrComp is located here. You should use Version 3.0 of blnStrComp