Table of contents
TOC
折叠目录
展开目录

Option Explicit 语句Option Explicit Statement

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

模块级别用于强制显式声明所有变量在该模块中。Used at module level to force explicit declaration of allvariables in thatmodule.

语法Syntax

明确的选项Option Explicit

备注如果使用,则选项显式语句必须出现在一个模块中所有过程之前。出现选项显式的模块中,您必须显式声明所有变量使用Dim专用公用ReDim,或静态语句。如果您尝试使用未声明的变量名,则在编译时出现错误。如果您不使用Option Explicit语句,所有未声明的变量是variant 类型的值类型的除非否则Def类型_语句使用指定的默认类型。Remarks If used, the OptionExplicit statement must appear in a module before anyprocedures. When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs atcompile time. If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Def_type statement.

请注意 使用Option Explicit来避免错误地键入现有变量的名称或避免代码中的混乱变量的范围不清楚。Note Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where thescope of the variable is not clear.

示例Example

此示例使用Option Explicit语句来强制显式声明所有变量。尝试使用未声明的变量将导致编译时错误。在模块级别仅使用Option Explicit语句。This example uses the Option Explicit statement to force explicit declaration of all variables. Attempting to use an undeclared variable causes an error at compile time. The Option Explicit statement is used at the module level only.

Option Explicit ' Force explicit variable declaration. 
Dim MyVar ' Declare variable. 
MyInt = 10 ' Undeclared variable generates error. 
MyVar = 10 ' Declared variable does not generate error. 
© 2018 Microsoft