2014-10-02 24 views
2

这是我从这段简单的代码中得到的独特结果。'Option Strict'是否依赖'Option Infer'来检测未声明的变量?

假设您想创建一个字符串类型的变量,而不是将其声明为字符串。你可以这样做,并没有得到来自编译器错误:

Option Strict On 

' Produces no errors: 
Dim MyString = "Random String" 

你也可以做到这一点,没有得到任何错误:

Option Infer Off 
' Produce no errors as well. 
Dim MyString = "Random String" 

但是,当你把两个选项字符串在选项推断关,有一个错误:

Option Strict On 
Option Infer Off 

' The following line generates an error - 
' Option Strict On requires all variable declarations to have an "As" clause 
Dim MyString = "Random String" 

为什么选项严格需要与结合选项推测?尤其是当错误明确指出以下错误是“Option Strict”类型时。为什么不能选项严格单独发现该行为错误?

+0

你是什么意思? – Eminem 2014-10-02 19:42:55

+0

请参阅[http://msdn.microsoft.com/de-de/library/bb384665.aspx](http://msdn.microsoft.com/de-de/library/bb384665.aspx)。有一张表可能包含这些声明的组合。 – Fratyx 2014-10-02 19:48:20

+0

由于缺少“As'子句,因此使用”option strict On“和”Infer Off“Dim MyString =”Random String“不会编译(与您的语句相反)。如果两者都是“关”,则字符串实际上是一个对象(如果您尝试使用“MyString.Length”,则可以看到它)。如果Infer为'On',则字符串文字被正确检测为字符串。 – 2014-10-02 19:53:24

回答

1

您忽略了您的项目级选项设置 - 它们将确定错误/警告,除非在文件级别被覆盖。查看这些项目属性的编译选项卡。

您的项目级选项推测可能设置为“开”,因此您的第一个示例实际上与包含“选项推断开”相同。

相关问题