VB.NET Turning Option Strict Off In-Line
Asked Answered
S

3

19

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.

Succuss answered 11/3, 2009 at 16:35 Comment(2)
Why do you think that you need to "cheat"? Any casting that is possible in non-strict mode can also be done in strict mode. The compiler just creates the code for you in non-strict mode.Haworth
@Guffa: What about late binding to COM objects? A simple [nostrict] someComObject.someMethod would be much more readable than using reflection. (And yes, there are cases where late binding is more appropriate than creating interop DLLs.)Nee
F
19

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.

Frecklefaced answered 11/3, 2009 at 16:43 Comment(1)
Thanks Mike. That's exactly what I've done. Yes, it is ugly, but stable.Succuss
B
4

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.

Beverle answered 11/3, 2009 at 16:41 Comment(0)
T
-3

A couple other ideas:

  • You could code with it mostly off and turn it on now and then to make sure the rest of your code complies
  • You could configure it so that Option Strict is ON for release builds but OFF for debug builds.
Tabu answered 11/3, 2009 at 16:51 Comment(1)
Joel: -1 because a) it will not help in this instance and b) it's an horrendously bad idea. It's not at all the same thing as switching on optimisations in a release build.Lavine

© 2022 - 2024 — McMap. All rights reserved.