Variables. IntroductionA 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:
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 |