SteffenS.
SteffenS.

Reputation: 9

Dim declaration with assignment and Option Strict

I have run into an issue with my code, as follows:

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

My issues are written as comments in the code above.

Even with a completely empty module, I am unable to enable the Option Strict option.

I think the solution is somewhere in Visual Studio's options.

Note: I have manually translated the error messages from German, so please expect a difference between the above and the official English versions.

Upvotes: 0

Views: 1728

Answers (1)

Bugs
Bugs

Reputation: 4489

Option Explicit and Option Strict must be set at the very top, followed by any Imports followed by the class itself followed by the methods:

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

This is the same for modules:

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

If you want to turn these options on or off for the entire project you can do so in the project properties:

Note that the settings in individual files (if any) will take precedence over the default settings set within the project properties.

Have a look at the documentation for more information on setting Option Explicit and Option Strict.

Upvotes: 3

Related Questions