gpt4 book ai didi

vb.net - 'Option Strict' 是否依赖 'Option Infer' 来检测未声明的变量?

转载 作者:行者123 更新时间:2023-12-04 18:06:08 27 4
gpt4 key购买 nike

这是我从这段简单的代码中得到的特殊结果。

假设您要创建一个字符串类型的变量,但不将其声明为字符串。您可以这样做并且不会从编译器中得到任何错误:

Option Strict On

' Produces no errors:
Dim MyString = "Random String"

您也可以这样做而不会出现任何错误:

Option Infer Off
' Produce no errors as well.
Dim MyString = "Random String"

但是,当您同时使用Option String OnOption Infer Off 时,会出现错误:

Option Strict On
Option Infer Off

' The following line generates an error -
' Option Strict On requires all variable declarations to have an "As" clause
Dim MyString = "Random String"

为什么 Option Strict 需要与 Option Infer 结合使用?特别是当错误明确指出以下错误是“Option Strict”类型时。为什么 Option Strict 不能单独将该行发现为错误?

最佳答案

您忽略了项目级别的选项设置 - 这些将确定错误/警告,除非在文件级别被覆盖。查看这些项目属性的编译选项卡。

您的项目级 Option Infer 可能设置为“On”,因此您的第一个示例实际上与包含“Option Infer On”相同。

关于vb.net - 'Option Strict' 是否依赖 'Option Infer' 来检测未声明的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com