Does 'Option Strict' rely on 'Option Infer' to detect undeclared variables?

298 Views Asked by At

Here are peculiar results I get from this simple piece of code.

Say you want to create a variable of type string, without declaring it as a string. You can do this and get no errors from the compiler:

Option Strict On

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

You can also do this and not get any errors:

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

But, when you combine both Option String On and Option Infer Off, there is an error:

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"

Why does Option Strict need to be combined with Option Infer? Especially when the error specifically says that the following error is an "Option Strict" type. Why can't Option Strict alone find that line as an error?

1

There are 1 best solutions below

0
On

You're ignoring your project-level Option settings - these will determine the errors/warnings unless overridden at the file level. Look at the compile tab of the project properties for these.

Your project-level Option Infer is probably set to 'On', so your first example is actually identical to including "Option Infer On".