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

避免命名争议Avoiding Naming Conflicts

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

当您尝试创建或使用以前定义的标识符时,将出现命名冲突。在某些情况下,命名冲突产生错误如" Ambiguous name detected"或""或" Duplicate declaration in current scope"。命名冲突检测不到,可能会导致产生错误的结果,您的代码中的 bug,尤其是如果您未显式声明所有变量之前第一次使用。A naming conflict occurs when you try to create or use an identifier that was previously defined. In some cases, naming conflicts generate errors such as " Ambiguous name detected" or "" or " Duplicate declaration in current scope". Naming conflicts that go undetected can result in bugs in your code that produce erroneous results, especially if you do not explicitly declare all variables before first use.

通过了解作用域、 专用模块级和公用模块级别,可以避免大多数命名冲突。You can avoid most naming conflicts by understanding the scoping, private module-level, and public module-level.

命名冲突可在标识符具有以下情形时出现:A naming conflict can occur when an identifier:

  • 在多个范围级别中可见。Is visible at more than one scoping level.

  • 在相同的级别具有两个不同的含义。Has two different meanings at the same level.

例如,在单独的模块中的过程可以具有相同的名称。因此,您可以将过程定义名为MySub在模块名为Mod1Mod2。如果仅从其自己的模块中的其他过程调用每个过程,没有冲突发生。但是,如果出现错误可以MySub从第三个模块,调用和没有资格可用于区分两个MySub过程。通过前面的每个标识符包含的模块名称限定符可以解决最命名冲突和如有必要,项目名称。例如:For example, procedures in separate modules can have the same name. Therefore, you can define a procedure named MySub in modules named Mod1 and Mod2. No conflicts occur if each procedure is called only from other procedures in its own module. However, an error can occur if MySub is called from a third module, and no qualification is provided to distinguish between the two MySub procedures. Most naming conflicts can be resolved by preceding each identifier with a qualifier that consists of the module name and, if necessary, a project name. For example:

YourProject.YourModule.YourSub MyProject.MyModule.MyVar

前面的代码中调用Sub过程YourSub,并将MyVar作为参数。您可以使用限定符的任意组合来区分相同的标识符。Visual Basic 匹配与匹配标识符"最近"声明的标识符的每个引用。例如,如果MyID声明为公共项目中的两个模块中 (Mod1Mod2),您可以指定MyID中声明Mod2内无条件Mod2,但必须符合其作为Mod2.MyID若要指定在Mod1.这也是如此如果Mod2在不同但直接引用项目中。但是,如果Mod2是在间接引用的项目中,即,直接引用,引用的项目所引用的项目Mod2变量名为MyID必须始终使用项目名称来限定。如果您引用了MyID第三,直接引用的模块,从匹配进行搜索时,遇到的第一个声明的:The preceding code calls the Sub procedure YourSub and passes MyVar as an argument. You can use any combination of qualifiers to differentiate identical identifiers. Visual Basic matches each reference to an identifier with the "closest" declaration of a matching identifier. For example, if MyID is declared Public in two modules in a project ( Mod1 and Mod2), you can specify the MyID declared in Mod2 without qualification from within Mod2, but you must qualify it as Mod2.MyID to specify it in Mod1. This is also true if Mod2 is in a different but directly referenced project. However, if Mod2 is in an indirectly referenced project, that is, a project referenced by the project you directly reference, references to the Mod2 variable named MyID must always be qualified with the project name. If you reference MyID from a third, directly referenced module, the match is made with the first declaration encountered by searching:

  • 直接被引用的项目,在工具菜单上的引用对话框中的显示顺序。Directly referenced projects, in the order that they appear in the References dialog box of the Tools menu.

  • 每个项目的模块。请注意,项目中的模块没有固有顺序。The modules of each project. Note that there is no inherent order to the modules in the project.

您不能重新使用宿主应用程序对象 (例如,在 Excel 中,在不同的范围级别的 R1C1) 的名称。You can't reuse names of host-application objects, for example, R1C1 in Microsoft Excel, at different scoping levels.

提示 命名冲突所致的典型错误包括不明确的名称、 重复的声明,未声明的标识符,找不到的过程。通过从开始使用Option Explicit语句来强制显式声明变量,然后它们才可以用于每个模块,可以避免一些潜在的命名冲突和标识符相关的错误。Tip Typical errors caused by naming conflicts include ambiguous names, duplicate declarations, undeclared identifiers, and procedures that are not found. By beginning each module with an Option Explicit statement to force explicit declarations of variables before they are used, you can avoid some potential naming conflicts and identifier-related bugs.

© 2018 Microsoft