As Table 6-2 shows, the ASCII values of lowercase alphabetical characters are greater than their uppercase counterparts: a is greater than A. The default in Visual Basic .NET is that string comparisons are case sensitive—that is, they distinguish whether a character is uppercase or lowercase. Consequently, jeff is greater than, rather than equal to, Jeff.
Depending on the context of your program, you may want to make case-insensitive comparisons—that is, comparisons in which whether a character is uppercase or lowercase is irrelevant. In validating a user who is attempting to log on, for example, usernames often are not case sensitive (whereas passwords usually are).
You use the Option Compare statement to declare the default comparison method to use when string data is compared. The Option Compare statement may be one, but only one, of the following:
Option Compare Binary
Option Compare Binary is the default, and is a case-sensitive comparison. Option Compare Text is a case-insensitive comparison. Thus, under Option Compare Binary, aaa is greater than AAA. However, under Option Compare Text, aaa is equal to AAA.
You can override the Option Compare setting by using the built-in functions UCase and LCase.
The UCase function takes a character or string as an argument. lt converts all lowercase letters to uppercase; all uppercase letters and nonletter characters remain unchanged. For example, if the string "Hello World 1234" is the argument for the UCase function, the function would return the string "HELLO WORLD 1234":
Debug.Write(Ucase("Hello World 1234")) ' Writes "HELLO WORLD 1234".
The LCase function is the converse of the UCase function, converting all uppercase letters to lowercase, with all lowercase letters and nonletter characters remaining unchanged. For example, if the string "Hello World 1234" is the argument for the LCase function, the function would return the string "hello world 1234".
The UCase and LCase functions may be used to override the Option Compare setting by converting the strings being compared to either all lowercase or all uppercase before making the comparison, thus eliminating case as a factor in the comparison.
The Option Compare statement must be declared at the module level, before any procedures.
Was this article helpful?