【问题标题】:Dim declaration with assignment and Option Strict带有赋值和 Option Strict 的暗淡声明
【发布时间】:2017-06-07 12:32:50
【问题描述】:

我的代码遇到了问题,如下:

Option Explicit Off

Option Strict On 'Results in Compiler Error. Expected base or compare or explicit or private

Sub DoSomething()

     Dim intOne As Integer 'This line works

     intOne = 1 'This line works

     Dim intTwo as Integer = 2 'Results in Compiler Error: Expected end of statement

End Sub

我的问题在上面的代码中写成 cmets。

即使模块完全为空,我也无法启用Option Strict 选项。

我认为解决方案在 Visual Studio 的选项中。

注意:我已经手动翻译了德语的错误信息,所以请期待上述和官方英文版本之间的差异。

【问题讨论】:

    标签: vb.net visual-studio compiler-errors option-strict option-explicit


    【解决方案1】:

    Option ExplicitOption Strict 必须设置在最顶部,然后是任何Imports,然后是类本身,然后是方法:

    Option Explicit On
    Option Strict On
    
    Imports System.Net
    
    Public Class Class1
    
        Private Sub DoSomething()
             Dim intOne As Integer
             intOne = 1
    
             Dim intTwo as Integer = 2 
        End Sub
    
    End Class
    

    模块也是如此:

    Option Explicit On
    Option Strict On
    
    Imports System.Net
    
    Module Module1
    
        Public Sub DoSomething()
    
            Dim intOne As Integer
            intOne = 1
    
            Dim intTwo As Integer = 2
        End Sub
    
    End Module
    

    如果您想为整个项目打开或关闭这些选项,您可以在项目属性中进行:

    请注意,单个文件中的设置(如果有)将优先于项目属性中设置的默认设置。

    有关设置Option ExplicitOption Strict 的更多信息,请查看文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2010-10-19
      相关资源
      最近更新 更多