.net Option Strict 和 Option Explicit 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2454552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:04:56  来源:igfitidea点击:

What do Option Strict and Option Explicit do?

.netvb.netoption-strictoption-explicit

提问by yretuta

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 Strictand Option Explicitanyway? I have googled it up but can't get the idea (because mostly it's Visual Basic, I'm doing PHP).

什么是Option StrictOption Explicit无论如何?我已经用谷歌搜索了它,但无法理解(因为主要是 Visual Basic,我在做 PHP)。

回答by TLiebe

Option Explicitmeans 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 Explicit意味着必须声明所有变量。见这里。如果没有这个,你可能会因为拼错另一个变量名而意外地声明一个新变量。当您尝试调试 VB 程序并找出您的程序无法正常工作的原因时,这是导致很多痛苦的事情之一。在我看来,这甚至不应该是一种选择——它应该始终开启。

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 Integerto a Byte). Again, an option that should be turned on by default.

Option Strict“将隐式数据类型转换限制为仅扩展转换”。见这里。启用此选项后,您不会意外地将一种数据类型转换为另一种不太精确的数据类型(例如从 anInteger到 a Byte)。同样,默认情况下应该打开一个选项。

回答by Matt Wilko

TL;DR

TL; 博士

Option Strictand Option Explicithelp you to catch potentialand actualerrors at design time, rather than your code compiling and failing at runtime. You should switch bothOn.

Option StrictOption Explicit帮助您在设计时捕捉潜在的实际的错误,而不是您的代码在运行时编译和失败。你应该切换两者On

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

Option Strict 和 Option Explicit 默认关闭。要打开它们:

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

选项严格 Tools -> Options -> Projects and Solutions -> VB defaults -> Option Strict。将其设置为On

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

选项显式 Tools -> Options -> Editor -> Require Variable Declaration。勾选它。

Option Explicit

选项显式

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

使用Option Explicit Off您不必在使用变量之前声明 (Dim) 变量:

a = 123 'a is automatically declared as an Integer

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 thinkis 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 Offyou can implicitly convert a datatype to a narrowing type with no error:

使用Option Strict Off,您可以将数据类型隐式转换为缩小类型而不会出错:

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.

对于这些情况,Option Strict 可作为对开发人员的警告,以确保 double 值永远不会超过Single.MaxValue

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

您还可以将 Enum 分配给不正确的值而不会出错。下面是一个真实的例子:

enter image description here

在此处输入图片说明

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.

变量应该设置为EOpticalCalStates.FAILED(24),实际上它将 State 设置为值 4,相当于EOpticalCalStates.ALI_HOR

Something like this is not easy to spot.

像这样的东西不容易发现。

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

因此,默认情况下您应该始终启用 Option Strict。此设置应该已设置为默认值,但 Microsoft 决定将其关闭以增加向后兼容性(事后看来,这是 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".

“项目”菜单 -> “属性...”项 -> “编译”选项卡 -> 将“选项严格”设置为“开”。

回答by danben

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

在此处查找详细信息: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.

Option Explicit 语句

默认情况下,Visual Basic .NET 或 Visual Basic 编译器强制执行显式变量声明,这要求您在使用之前声明每个变量。要更改此默认行为,请参阅更改默认项目值部分。

Option Strict 语句

默认情况下,Visual Basic .NET 或 Visual Basic 编译器不强制执行严格的数据类型化。要更改此默认行为,请参阅更改默认项目值部分。