VB.NET 选项比较文本在 .NET 3.5 中无法正常工作
我正在使用 VB.NET 开发旧版 Windows 窗体,最近发现了一个令人困惑的行为。
有一个项目 A 面向 .NET 2.0,并在代码文件中设置了选项比较文本。项目 A 包含使用 String.InStr 方法的函数 CharacterFilter。 CharacterFilter 将从给定字符串中删除特殊字符。
项目 B 引用项目 A。项目 B 面向 .NET 3.5,并且还在其代码文件中设置了 Option Compare Text。
给定输入字符串“123”并且要删除的特殊字符是“²³”,令人惊讶的是,CharacterFilter 函数不会从给定输入字符串中删除“23”。但是,如果项目 B 以 .NET 4.0 为目标,则将从给定的输入字符串中删除“23”。
从 Microsoft 文档来看,无论 .net 框架版本如何,设置选项比较文本都应删除“23”。 https:// learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/option-compare-statement
有人可以帮忙指出报告此行为的 Microsoft 文档吗?非常感谢。
--Project A targets .NET 2.0
Option Compare Text
Public Class Backtool
Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
Dim i As Integer
Dim vF As String
Dim vPos As Integer
For i = Len(Filter_Renamed) To 1 Step -1
vF = Mid(Filter_Renamed, i, 1)
vPos = InStr(Text, vF)
Do While vPos > 0
Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
vPos = InStr(Text, vF)
Loop
Next i
CharFilter = Text
End Function
End Class
--Project B that references project A
Option Compare Text
Module Module1
Public BT As Backtool
Sub Main()
BT = New Backtool()
Dim text = "123"
Dim filterStr = "²³"
Console.WriteLine("Result string is: " + BT.CharFilter(text, filterStr, False))
--Returns "123" if targets .NET Framework 3.5
--Returns "1" if targets .NET Framework 4.0
End Sub
End Module
I am working on a legacy Windows Form with VB.NET and I have recently discovered a confusing behavior.
There is project A that targets .NET 2.0 with Option Compare Text set at code file. Project A contains function CharacterFilter that uses method String.InStr. CharacterFilter will remove special characters from given string.
Project B references to project A. Project B targets .NET 3.5 and also set Option Compare Text at its code file.
Given input string as "123" and special characters to be removed are "²³", function CharacterFilter surprisingly does not remove "23" from given input string. However, if Project B is targetted .NET 4.0, "23" will be removed from given input string.
From Microsoft docs, setting Option Compare Text should remove "23" no matter what .net framework versions.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/option-compare-statement
Could someone help to point a Microsoft document reporting this behavior. Many thanks.
--Project A targets .NET 2.0
Option Compare Text
Public Class Backtool
Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
Dim i As Integer
Dim vF As String
Dim vPos As Integer
For i = Len(Filter_Renamed) To 1 Step -1
vF = Mid(Filter_Renamed, i, 1)
vPos = InStr(Text, vF)
Do While vPos > 0
Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
vPos = InStr(Text, vF)
Loop
Next i
CharFilter = Text
End Function
End Class
--Project B that references project A
Option Compare Text
Module Module1
Public BT As Backtool
Sub Main()
BT = New Backtool()
Dim text = "123"
Dim filterStr = "²³"
Console.WriteLine("Result string is: " + BT.CharFilter(text, filterStr, False))
--Returns "123" if targets .NET Framework 3.5
--Returns "1" if targets .NET Framework 4.0
End Sub
End Module
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经在 Visual Studio 2010 中重现了您的项目,并且出现了同样的奇怪错误。我读过微软的文档,但没有找到原因。
但是,我找到了解决方法...尝试在
Microsoft 规范化表单文档下面的
ProjectB 中使用
System.Text.NormalizationForm
https://learn.microsoft.com/en-us/dotnet/api/system.text.normalizationform?view=netframework-2.0我希望它有用。
I have reproduce your projects in Visual Studio 2010 and I have the same stranger error. I have read Microsoft documentation but I can't find the reason.
But, I'v found a work around... try to use
System.Text.NormalizationForm
inside the ProjectBBelow Microsoft normalizationForm documentation
https://learn.microsoft.com/en-us/dotnet/api/system.text.normalizationform?view=netframework-2.0
I hope it is useful.