Private (statement)

Syntax

Private name [(subscripts)] [As type] [,name [(subscripts)] [As type]]...

Description

Declares a list of private variables and their corresponding types and sizes.

Comments

Private variables are global to every Sub and Function within the currently executing script.

If a type-declaration character is used when specifying name (such as %, @, &, $, or !), the optional [As type] expression is not allowed. For example, the following are allowed:

  Private foo As Integer
  Private foo%

The subscripts parameter allows the declaration of arrays. This parameter uses the following syntax:

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

The lower and upper parameters are integers specifying the lower and upper bounds of the array. If lower is not specified, then the lower bound as specified by Option Base is used (or 1 if no Option Base statement has been encountered). Up to 60 array dimensions are allowed.

 

The total size of an array (not counting space for strings) is limited to 64K.

Dynamic arrays are declared by not specifying any bounds:

  Private a()

The type parameter specifies the type of the data item being declared. It can be any of the following data types: String, Integer, Long, Single, Double, Currency, Object, data object, built-in data type, or any user-defined data type.

If a variable is seen that has not been explicitly declared with either Dim, Public, or Private, then it will be implicitly declared local to the routine in which it is used.

Fixed-Length Strings

Fixed-length strings are declared by adding a length to the String type-declaration character:

  Private name As String * length

where length is a literal number specifying the string's length.

Initial Values

All declared variables are given initial values, as described in the following table:

 

Data Type

Description

 

Integer

0

 

Long

0

 

Double

0.0

 

Single

0.0

 

Currency

0.0

 

Object

Nothing

 

Date

December 31, 1899 00:00:00

 

Boolean

False

 

Variant

Empty

 

String

"" (zero-length string)

 

User-defined type

Each element of the structure is given a default value, as described above.

 

Arrays

Each element of the array is given a default value, as described above.

Example

This example sets the value of variable x# in two separate routines to show the behavior of private variables.

Private x#

Sub Area()
  x# = 10  'Set this copy of x# to 10 and display
  MsgBox x#
End Sub

Sub Main()
  x# = 100  'Set this copy of x# to 100 and display after calling the Area subroutine
  Area
  MsgBox x#
End Sub

See Also

Dim (statement); Redim (statement); Public (statement); Option Base (statement).

More information

P