Public Statement

Description

Used at module level to declare public variables and allocate storage space.

Syntax

Public varname[([subscripts])][As type][,varname[([subscripts])][As type]] . . .

The Public statement syntax has these parts:

Part

Description

varname

Name of the variable; follows standard variable naming conventions.

subscripts

Dimensions of an array variable; up to 60 multiple dimensions may be declared. The argument subscripts uses the following syntax:

[lower To] upper [,[lower To] upper] . . .

type

Data type of the variable; may be Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined.


Remarks

Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are Public only within the project in which they reside.

Use the Public statement to declare the data type or object type of a variable. For example, the following statement declares a variable as an Integer:


Public NumberOfEmployees As Integer

If you do not specify a data type or object type and there is no Deftype statement in the module, the variable is Variant by default.

When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string, and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it was a separate variable. A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.

You can also use the Public statement with empty parentheses to declare dynamic arrays. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Private, Public or Dim statement, an error occurs.

See Also

Array Function, Const Statement, Dim Statement, Option Base Statement, Option Private Statement, Private Statement, ReDim Statement, Static Statement, Type Statement.

Example

The Public statement is used at the module-level to declare variables as public; that is, they are available to all procedures in all modules in all applications unless Option Private Module is in effect.


Public Number As Integer    ' Public integer variable.
Public NameArray(1 To 5) As String    ' Public array variable.
Public MyVar, YourVar, ThisVar As Integer    ' Multiple declarations.