Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Turning Option Strict Off In-Line

Is there a way to turn option strict off for just a single line of code?

I'm doing some maintenance work and I need to "cheat" in just one place and I don't want to lower the standard for the entire file.

like image 385
Jeff Avatar asked Mar 11 '09 16:03

Jeff


People also ask

How do I turn off Option Strict in Visual Basic?

Option Strict and Option Explicit are Off by default. To switch them on: Option Strict Tools -> Options -> Projects and Solutions -> VB defaults -> Option Strict .

What happens if Option Strict is turned off?

If Option Strict is off (the default), the variable is set to Nothing . If Option Strict is on, a compile-time error occurs. If Option Infer is on (the default), the variable takes the data type of the initializer.

What does Option Strict On mean in VB net?

VB.NET Option Strict [On | Off] Option Strict is prevents program from automatic variable conversions, that is implicit data type conversions .

What are Option Strict and Option Explicit in VB net?

The Explicit option requires that all variables used in the code are declared before they're used. The Strict option requires that variables are declared with a specific type. In other words, the Strict option disallows the use of generic variables that can store any data type.


2 Answers

Sadly, it is not possible for a single line of code in a file. See the MSDN docs.

On the other hand, you could probably make your single line of code a separate function, put that in a new file with partial class attributes, and put Option Strict Off on that one file. The IL compiler will probably inline your function anyway, so it will be equivalent speedwise, but will be ugly from a practical point of view.

like image 180
Mike Avatar answered Oct 17 '22 09:10

Mike


Since it must appear in the declarations section of the module then option strict can't be used in the middle of code. But it can be done on a per-module basis which might help a little.

And there is no mention in the "Visual Basic 2005 in a nutshell" book that suggests there's another method of turning it on or off.

like image 39
RobS Avatar answered Oct 17 '22 09:10

RobS