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」類型時。爲什麼不能選項嚴格單獨發現該行爲錯誤?
你是什麼意思? – Eminem 2014-10-02 19:42:55
請參閱[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
由於缺少「As'子句,因此使用」option strict On「和」Infer Off「Dim MyString =」Random String「不會編譯(與您的語句相反)。如果兩者都是「關」,則字符串實際上是一個對象(如果您嘗試使用「MyString.Length」,則可以看到它)。如果Infer爲'On',則字符串文字被正確檢測爲字符串。 – 2014-10-02 19:53:24