Saturday, May 26, 2007

VBScript Variable

In VBScript all variables are of the type variant that can store any type of value.

The name of the variable in VBScript must begin with a letter and it cannot contain period (.) and its maximum length can be of 255 characters.

It must be distinctive (unique) within the scope in which it is declared. If we declare a variable inside a procedure then its scope is local to that procedure and only code within that procedure can access it, but if we declare a variable outside a procedure in a script, it is available to all the procedures in a script. Procedure level variable exits as long as we are in the procedure and a life of a script level variable is the time it is declare up till the time script finishes.

Variables can be declared explicitly and implicitly.

Explicitly variables are declared with Dim statement, Public Statement, Private Statement.

Dim Name

Dim Name, employee_address, city

Implicitly we can declare them within the script by just using the variable name. But this practice is prone to errors.

We can compel VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script.

VBScript does not provide support for constants.

From VBScript 5.0 onwards programmers are able to create class constructs in VBScript-A step towards OOP.

variables declared by Dim and Public are public in nature (accessible outside of the class). By default also variables are Public in nature. With Private we can declare variables not visible out side of the class.

Example of a variable:

Enter this in notepad, save the notepad with some name and .html extension (like I saved it in c:\pro\a.html where pro is a name of a folder)

VBScript Variable

Now open internet explorer and in the address bar type c:\pro\a.html and press enter.

Another example that you can try is:

VBScript Variable

Another example of getting input from the user:

Enter the below code in notepad and save it with .vbs extension (like I saved it as c:\pro\c.vbs where pro is a name of a folder)

dim variable_name

variable_name =InputBox("Enter your name:")
MsgBox("Your name is " & variable_name)

Now go to command prompt(C:\>) and type pro\c and hit enter ( no need to type extension)