-4

出于调试目的,是否可以Option Compare在运行时获取 的值?

(我正在开发一个旧的Windows 窗体应用程序,它使用InStr和第一个参数作为String(Instr 的三参数版本),没有第三个参数,然后由Option Compare确定。Option Compare Text据说使 InStr 不区分大小写;默认值为Option Compare Binary.)

4

1 回答 1

6

The option setting is only known at compile time. The compiler handles this with the OptionCompareAttribute attribute. When present on an optional parameter, it substitutes the option setting. Which inspires this function:

Function GetOptionCompare(<CompilerServices.OptionCompare()> _
                          Optional ByVal Compare As CompareMethod = CompareMethod.Binary) As CompareMethod
    Return Compare
End Function

This however doesn't work for unguessable reasons. Punt the problem, simply take advantage of the difference between the compare methods:

Function GetOptionCompare() As CompareMethod
    Return IIf("A" = "a", CompareMethod.Text, CompareMethod.Binary)
End Function

Do beware that Option Compare can be a per-source file setting, this function can only work when you rely on the Visual Studio default. That's a bit iffy. Well, pretty iffy. Well, it's bad. Avoid having to want to know the setting.

于 2012-04-04T12:27:53.530 回答