5.2.3.1 Module Variable Declaration Lists

module-variable-declaration = public-variable-declaration / private-variable-declaration

  
 global-variable-declaration = "Global"  variable-declaration-list 
 public-variable-declaration = "Public" ["Shared"] module-variable-declaration-list 
 private-variable-declaration = ("Private" / "Dim") [ "Shared"] module-variable-declaration-list 
  
 module-variable-declaration-list = (withevents-variable-dcl / variable-dcl) 
       *( "," (withevents-variable-dcl  / variable-dcl) ) 
 variable-declaration-list = variable-dcl *( "," variable-dcl ) 

<global-variable-declaration> and the optional Shared keyword (section 3.3.5.1) provides syntactic compatibility with other dialects of the Basic language and/or historic versions of VBA.

Static Semantics

  • The occurrence of the keyword Shared has no meaning.

  • Each variable (section 2.3) defined within a <module-variable-declaration> contained within the same module (section 4.2) MUST have a different variable name (section 2.3).

  • Each variable defined within a <module-variable-declaration> is a module variable and MUST have a variable name that is different from the name of any other module variable, module constant, enum member, or procedure (section 2.4) that is defined within the same module.

  • A variable declaration that is part of a <global-variable-declaration> or <public-variable-declaration> declares a public variable. The variable is accessible within the enclosing project (section 4.1). If the enclosing module is a class module (section 4.2) or is a procedural module (section 4.2) that is not a private module (section 5.2.1.4), then the variable is also accessible within projects that reference the enclosing project.

  • A variable declaration that is part of a <private-variable-declaration> declares a private variable. The variable is only accessible within the enclosing module.

  • If a variable defined by a <public-variable-declaration> has a variable name that is the same as a project name (section 4.1) or a module name (section 4.2) then all references to the variable name MUST be module qualified unless they occur within the module that contains the <public-variable-declaration>

  • A variable defined by a <module-variable-declaration> can have a variable name that is the same as the enum name of a <enum-declaration> defined in the same module but such a variable cannot be referenced using its variable name even if the variable name is module qualified.

  • If a variable defined by a <public-variable-declaration> has a variable name that is the same as the enum name of a public <enum-declaration> in a different module, all references to the variable name MUST be module qualified unless they occur within the module that contains the <public-variable-declaration>.

  • The declared type (section 2.2) of a variable defined by a <public-variable-declaration> in a <class-module-code-section> might not be a UDT (section 2.1) that is defined by a <private-type-declaration> or a private enum name.

  • A <module-variable-declaration-list> that occurs in a procedural module MUST NOT include any <withevents-variable-dcl> elements.

 

Runtime Semantics.

  • All variables defined by a <module-variable-declaration> that is an element of in a <procedural-module-declaration-section> have module extent (section 2.3).

  • All variables defined by a <module-variable-declaration> that is an element of in a <class-module-declaration-section> are member (section 2.5) variables of the class (section 2.5) and have object extent (section 2.3). Each instance (section 2.5) of the class will contain a distinct corresponding variable.