Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

Variables. Introduction


A variable is a named location in computer memory that you can use for data storage during the execution of your scripts. You can use variables to store input from the user gathered via your web page, save data returned from functions, or hold results from calculations. Exactly how, and where, variables are stored is not important. What you use them for, and how you use them is important. Let's look at a simple VBScript example to clarify the use of variables.

<html>
<head>
<script language="VBScript">
sub VariablesDemo 'Start a subroutine
Dim SomeVariable 'Variable declaration
SomeVariable=InputBox("Enter your name")
'A variable returned by function
MsgBox "Your name is " & SomeVariable
'Using this variable to display your name
End Sub 'end of procedure
</script>
</head>
<body onLoad="VariablesDemo">'Invoking the subroutine
</body>
</html>

This subroutine is using two intrinsic functions (InputBox and MsgBox). The first (InputBox) function invokes new window where you enter you name. The MsgBox function displays the result.

Declaring Variables

There are two methods for declaring variables in VBScript, explicitly and implicitly. To declare variables explicitly, the Dim statement is used:

Dim Name

The above statement declares the variable Name. One can also declare multiple variables on one line as shown below.

Dim Name,Age,ID

Variables can be also declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug. You can force VBScript to require all variables to be explicitly declared by including the statement

Option Explicit

at the start of every script. Any variable that is not explicitly declared will then generate an error. It helps debugging the web-page. VBScript has the following rules that must be followed when creating a variable:

  • A variable name must begin with an alphabetic character (this can be either an uppercase or lowercase character, A through Z);
  • The remaining characters in the variable name can be either alphabetic characters (A through Z), numbers (0-9), or underscores (_).
  • A variable name cannot contain an embedded period.
  • The name cannot be longer than 255 characters.
  • The variable name cannot be a VBScript reserved word. The reserved words include the names of functions, operators, properties, statements, and keywords.
  • The variable must be unique in the scope in which it is declared. For example, if it is declared within a function, it must be the only variable in that function with that name to avoid confusion. You can have a variable in another function with the same name, as long as both variables are procedure level (only available within the procedure where they are declared).
  • The copyright of the article Variables. Introduction in VB Script is owned by Maxim Karetnikov. Permission to republish Variables. Introduction in print or online must be granted by the author in writing.

    Go To Page: 1 2

    Articles in this Topic    Discussions in this Topic