Option Strict 和 Option Explicit 有何作用?
我看到了这个帖子:
打字错误…请使用严格和明确的选项..在我作为顾问参与的一个软件开发项目中,他们到处都遇到了荒谬的错误…结果开发人员无法拼写并且会用以下方式声明变量拼写错误..没什么大不了的,直到您在为其分配值时使用正确的拼写...并且您可以选择显式关闭。哎哟他们......”
到底什么是Option Strict
和Option Explicit
?我已经用谷歌搜索了它但无法理解(因为主要是Visual Basic,我是做PHP)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Option Explicit
表示所有变量都必须声明。请参阅此处。如果没有这个,您可能会因为拼写错误另一个变量名而意外地声明一个新变量。当您尝试调试 VB 程序并找出程序无法正常工作的原因时,这是导致很多痛苦的事情之一。在我看来,这甚至不应该是一个选项 - 它应该始终打开。Option Strict
“将隐式数据类型转换限制为仅扩展转换”。请参阅此处。启用此选项后,您就不会意外地将一种数据类型转换为另一种不太精确的数据类型(例如,从Integer
到Byte
)。同样,默认情况下应打开一个选项。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 anInteger
to aByte
). Again, an option that should be turned on by default.TL;DR
Option Strict
和Option Explicit
帮助您在设计时捕获潜在和实际错误,而不是您的代码编译并在运行时失败。您应该将两者切换为开
。默认情况下,Option Strict 和 Option Explicit 处于关闭状态。打开它们:
Option Strict
工具 ->选项->项目和解决方案 -> VB默认值->选项严格
。将其设置为开
。选项显式
工具 ->选项->编辑->需要变量声明
。勾选它。Option Explicit
使用Option Explicit Off,您不必在使用变量之前声明(Dim)变量:
a = 123 'a 自动声明为整数
这会变得危险当您在一个地方声明一个变量并认为稍后会使用它但输入错误时:
或者更糟糕的是,您为您认为在范围内的变量分配了一个值,但事实并非如此t 并且您最终声明了一个具有相同名称但作用域不同的新变量。
对于大量代码或长方法,这些很容易被忽略,因此您应该始终打开它以防止出现此类问题。
Option Strict
使用Option Strict Off,您可以隐式地将数据类型转换为缩小类型,而不会出现错误:
对于这些情况,Option Strict 会向开发人员发出警告,以确保双精度值永远不会超过 <代码>Single.MaxValue。
您还可以将 Enum 分配给不正确的值,而不会出现错误。以下是一个真实的示例:
该变量应设置为
EOtticalCalStates.FAILED
(24),实际上它将 State 设置为值 4,相当于EOopticalCalStates.ALI_HOR
。像这样的东西并不容易被发现。
因此,默认情况下您应该始终启用 Option Strict。此设置应该设置为默认值,但微软决定将其保留以提高向后兼容性(事后看来,这是一个错误)。
如果您在为新项目设置默认值之前已经启动了一个项目,则需要使用:
“项目”菜单 -> “属性...”项-> “编译”选项卡->将“选项严格”设置为“开”。
TL;DR
Option Strict
andOption Explicit
help you to catch potential and actual errors at design time, rather than your code compiling and failing at runtime. You should switch bothOn
.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 toOn
.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:
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:
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 toEOpticalCalStates.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".
请在此处查找详细信息:http://support.microsoft.com/kb/311329
Find details here: http://support.microsoft.com/kb/311329