VB.NET:选项比较文本和泛型 [英] VB.NET: Option Compare Text and Generics
问题描述
我无法弄清楚为什么这不起作用。
I can't figure out why this doesn't work.
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:
AreEqual(Of String)("Hello", "HELLO")
我希望上面的调用返回TRUE,因为Option Compare Text是组。 MSDN说( http://msdn.microsoft.com/en-us/library/ 8t3khw5f(VS.80).aspx [ ^ ]:
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[^]:
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.
然而,它返回假。有任何想法吗? (也尝试在我的AreEquals方法中使用调试器中的a = b,仍然说它是假的)
Yet, it returns FALSE. Any ideas? (also tried to use "a=b" in the debugger in my "AreEquals" method, still says it's FALSE)
推荐答案
使用选项与string.equals比较不会工作,但将比较方法更改为下面的示例将产生您之后的结果
使用选项比较示例
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
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
请注意使用选项比较在它自己的默认为二进制和上面的例子不会工作,因为它的排序顺序不同于文本
,如您提供的msdn文章所示,备注文章的部分文本比较它不区分大小写,但二进制比较是例如A< B< a< b
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
Dave答案的进一步证明
MSDN String.equals
Further proof to Dave's answer
MSDN String.equals
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) comparison
因为a不等于b。
你需要设置一个上限和比较。
Because a does not equal b.
You would need to set a to caps and compare.
这篇关于VB.NET:选项比较文本和泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!