O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Public Statement

Syntax

Public varname[([subscripts])] _
   varname[([subscripts])]
varname

Use: Required

Data Type: Any

The name of the variable, which must follow VBScript naming conventions (see the second bullet in “Rules at a Glance”).

subscripts

Use: Optional

Data Type: Integer or Long

Denotes varname as an array and optionally specifies the dimensions and number of elements of the array.

Description

Used in a script or a Class block to declare a public variable and allocate the relevant storage space in memory. A Public variable has global scope—that is, it can be used by all procedures in a script. When used in a class construct, it is visible outside the class project.

Rules at a Glance

  • The behavior of a Public variable depends on where it’s declared, as the following table shows:

Variable declared in...

Scope

Any procedure, Function or Property statement

Illegal; generates a syntax error; use the Dim statement instead.

Global code

Variable is available throughout the script.

Class block declarations section

Variable is available as a property of the class to all code within the script.

  • You can override standard variable naming conventions by placing your variable name in brackets. This allows you to use reserved words or illegal characters in variable names. For example:

    Public [me]
    Public [1Var]
    Public [2-Var]
  • varname follows standard VB naming conventions. It must begin with an alphabetic character, can’t contain embedded periods or spaces, can’t be the same as a VBScript reserved word, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required