Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can't figure out why this doesn't work.

VB
Option Compare Text
..
Private Shared Function AreEqual(Of T)(ByVal a As T, ByVal b As T) As Boolean
    If a Is Nothing AndAlso b Is Nothing Then
        Return True
    ElseIf a IsNot Nothing AndAlso b IsNot Nothing AndAlso a.Equals(b)  Then
        Return True
    Else
        Return False
    End If
End Function


Suppose I call:
VB
AreEqual(Of String)("Hello", "HELLO")


I would expect the call above to return TRUE because the "Option Compare Text" is set. MSDN says (http://msdn.microsoft.com/en-us/library/8t3khw5f(VS.80).aspx[^]:

CSS
Option Compare Statement
Declares the default comparison method to use when comparing string data.

Copy
Option Compare { Binary | Text }
Parts
--------------------------------------------------------------------------------
Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.



Yet, it returns FALSE. Any ideas? (also tried to use "a=b" in the debugger in my "AreEquals" method, still says it's FALSE)
Posted
Updated 2-Sep-10 10:31am
v4

using option compare with string.equals wont work, but changing the compare method to the example below will produce the results that you are after

using option compare example

VB
option compare text

Public Class Form2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String = "Hi"
        Dim b As String = "HI"
        If a = b Then
            MessageBox.Show("TRUE")
        Else
            MessageBox.Show("FALSE")
        End If
    End Sub

End Clas


Please note that using option compare on its own defaults to binary and the above example wont work as its sort order is different to text

as shown by the msdn article that you have provided, the remarks section of the article with Text compare it isn't case sensitive, but binary compare is e.g. A < B < a < b
 
Share this answer
 
v2
Further proof to Dave's answer

MSDN String.equals

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison
 
Share this answer
 
Comments
Dalek Dave 1-Sep-10 10:10am    
Thanks Mr Whale, I shall also uplift your tally!
sparky2778 2-Sep-10 16:22pm    
Why should .Equals be case sensitive when the following is from MSDN:
(http://msdn.microsoft.com/en-us/library/8t3khw5f(VS.80).aspx)

Option Compare

Declares the default comparison method to use when comparing string data.

Option Compare { Binary | Text }
Parts
--------------------------------------------------------------------------------
Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.


Binary
Optional. Results in string comparisons based on a sort order derived from the internal binary representations of the characters.
Because a does not equal b.

You would need to set a to caps and compare.
 
Share this answer
 
Comments
Simon_Whale 1-Sep-10 10:07am    
Reason for my vote of 5
because I can and its the correct answer.. string.equals performs a case sensitive comparison
khkhkjh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900