VBScript Basics – VBS variable definition and usage


1、 Declarative variable

① Explicit declaration: declared with dim, public and private statements

② Implicit declaration: use directly without declaration

③ Mandatory declaration: the option explicit statement forces all variables to be explicitly declared

dim name,age
msgbox name & age
'name = "ethon": age = 27 assign values to multiple variables
Note: references to variables, methods, functions, and objects in VBScript are case insensitive
'implicit declaration
msgbox hello

2、 Variable scope

There are three variable scopes in VBScript:

① Script level scope

Variables are valid throughout the script file. The scope of the declared variable is the entire script.

② Process level scope

Variables are valid in procedures or functions. Code other than procedures and functions cannot access procedure level variables.

③ Class level scope

This is a special structure that contains logical grouping of attributes and methods. Code outside the class definition cannot access class level variables.

There are three statements that can be used to declare variables:DimPrivatePublic

Variables are declared with different statements in different situations, depending on the scope of the variable:

① Dim: variables used to declare scripts, procedures, and class level scopes

1) All variables declared as script level variables are valid in the whole script file, whether dim, private or public are used;

2) For process variables, dim must be used;

3) For class level variables, the effect of dim is exactly the same as that of public;

② Private: used to declare scripts, class level scopes, and use private statements

1) If it is used for script level variables, its function is exactly the same as dim and public;

2) In order to declare a private class level variable, you must use private;

③ Public: used to declare scripts and class level scopes

1) Declare the variable of script level scope, which is the same as dim or private in effect;

2) The declared class level variables are the public properties of this class. All variables declared with dim or public at the class level are a valid public attribute in the whole class.

How to declare variables correctly

1) Variables declared with dim in a procedure are local to that procedure

At the script level, variables are declared with dim, which is a full-featured keyword for declaring variables.

In scripts that are not class based or windows script components, the effects of private and public are no different from dim.

2) If you like, you can declare variables that are valid in the whole script with private (instead of dim) in the script.

The use of private becomes more important when declaring variables belonging only to the class in the class.

Public is only used to declare public properties of classes. You can also consider using private variables with property let, set and get procedures. Although dim has exactly the same effect as public in the class, it still prefers not to use dim in the class.