What do Option Strict and Option Explicit do?

回眸只為那壹抹淺笑 提交于 2019-11-25 22:57:47

问题


I saw this post:

Typos… Just use option strict and explicit please.. during one software development project, which I was on as a consultant, they were getting ridiculous amounts of errors everywhere… turned out the developer couldn’t spell and would declare variables with incorrect spelling.. no big deal, until you use the correct spelling when you’re assigning a value to it… and you had option explicit off. Ouch to them…\"

What is Option Strict and Option Explicit anyway? I have googled it up but can\'t get the idea (because mostly it\'s Visual Basic, I\'m doing PHP).


回答1:


Option Explicit means that all variables must be declared. See here. Without this, you can accidentally declare a new variable just by misspelling another variable name. This is one of those things that cause a lot of grief as you're trying to debug VB programs and figure out why your program isn't working properly. In my opinion, this shouldn't even be an option - it should always be on.

Option Strict "restricts implicit data type conversions to only widening conversions". See here. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.




回答2:


TL;DR

Option Strict and Option Explicit help you to catch potential and actual errors at design time, rather than your code compiling and failing at runtime. You should switch both On.

Option Strict and Option Explicit are Off by default. To switch them on:

Option Strict Tools -> Options -> Projects and Solutions -> VB defaults -> Option Strict. Set it to On.

Option Explicit Tools -> Options -> Editor -> Require Variable Declaration. tick it.

Option Explicit

With Option Explicit Off you don't have to declare (Dim) a variable before using it:

a = 123 'a is automatically declared as an Integer

This becomes dangerous when you declare a variable in one place and think you are using it later but mis-type it:

Dim counter As Integer = 0
'some lines later...
countr = 55 'This creates a new variable called countr 

Or even worse, you assign a value to a variable that you think is in scope, but it isn't and you end up declaring a new variable with the same name but differing scope.

With a lot of code or long methods these can be easy to miss so you should always switch it on to prevent these sorts of issues.

Option Strict

With Option Strict Off you can implicitly convert a datatype to a narrowing type with no error:

Dim d As Double = 999.99
Dim s As Single = d 'No error with Option Strict Off

For these cases Option Strict serves as a warning to the developer to make sure that the double value should never exceed Single.MaxValue.

You can also assign an Enum to the incorrect value with no error. The following is a real example of this:

The variable should have been set to EOpticalCalStates.FAILED (24), in fact it sets the State to a value of 4 which is equivalent to EOpticalCalStates.ALI_HOR.

Something like this is not easy to spot.

Therefore you should always have Option Strict on by default. This setting should have been set on as default, but Microsoft decided to leave it off to increase backwards compatibility (which with hindsight was a mistake IMO).


If you have started a project before setting the default for new projects, you will need to use:

"Project" menu -> " Properties..." item -> "Compile" tab -> set "Option strict" to "On".




回答3:


Find details here: http://support.microsoft.com/kb/311329

The Option Explicit statement

By default, the Visual Basic .NET or Visual Basic compiler enforces explicit variable declaration, which requires that you declare every variable before you use it. To change this default behavior, see the Change the Default Project Values section.

The Option Strict statement

By default, the Visual Basic .NET or Visual Basic compiler does not enforce strict data typing. To change this default behavior, see the Change the Default Project Values section.



来源:https://stackoverflow.com/questions/2454552/what-do-option-strict-and-option-explicit-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!