| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
When a variable declared it reserved a space in computer memory that can be use for
storaging data during the execution of scripts. You can use variables to:
- Store input from the user via web page
- Save data returned from procedures and functions
- Hold results from calculations
|
|
Declaring Variables
|
|
Dim statement, the Public statement, and the Private statement are used to declare variables in script, explicitly. For example:
|
You also declare multiple variables by separating each variable name with a comma. For example:
|
|
Dim Left, Right,Top, Bottom.
|
You can also declare a implicit variable by simply using variable name inside script. But it is not a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.
|
|
Naming limitations
|
- Must begin with a letter
- Cannot contain a period (.)
- Cannot exceed 255 characters
- They must be unique within the same scope.
|
|
Variants and Subtypes
|
|
VBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The following table describes the subtypes supported by VBScript.
|
| Subtype |
Description of Uses for Each Subtype |
| Byte |
Integer numbers between 0 to 255 |
| Boolean |
True and
False |
| Currency |
Monetary values |
| Date |
Date and time |
| Double |
Extremely large numbers with decimal points |
| Empty |
The value that a variant holds before being used |
| Error |
An error number |
| Integer |
Large integers between -32,768 and 32,767 |
| Long |
Extremely large integers (-2,147,483,648 and
2,147,483,647) |
| Object |
Objects |
| Null |
No valid data |
| Single |
Large numbers with decimal points |
| String |
Character strings |
|
Assigning Values
|
To assign a value to the variable
Variable_name = value
The following examples demonstrate how to assigning values to variables:
Name = "Larry Roof"
HoursWorked = 50
Overtime = True
|
|
Scope and Lifetime of Variables
|
|
Lifetime means How long a variable exists
When you declare a variable inside a procedure, the variable can only be
accessed within that procedure. When the control of program reach outside the procedure, the variable is
destroyed. These variables are called local variables. You can have local
variables with the same name in different procedures, because each is recognized
only by the procedure in which it is declared.
All the procedures can access the variable if varibles declared in outside a procedure.
The lifetime of these variables starts when they are declared on page load time
and ends when the page is closed.
|
|
The following example descibe below demonstrates both script-level and procedure-level variables.
|
|
<SCRIPT>
Dim counter
Sub cmdButton_onClick
Dim temp
End Sub
</SCRIPT>
|
|
The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure.
|
|
Constants
|
|
VBScript does not provide support for constants, such as other programming languages. You can work by assigning values to variables that you have defined as shown given example. Here, TAX_RATE is our constant.
|
|
<SCRIPT>
Dim TAX_RATE
TAX_RATE = .06
Function CalculateTaxes
CalculateTaxes = CostOfGoods * TAX_RATE
End Function
</SCRIPT>
|
|
Scalar Variables and Array Variables
|
|
A variable containing a single value is a scalar variable.
To assign more than one related value to a single variable
you can create a variable that can contain a series of values.
This is called an array variable. Scalar variables and Array variables are declared in the same way,
except that the declaration of an array variable uses parentheses ( ) following the variable name.
In the following example, a single-dimension array containing 10 elements is declared:
|
|
Although the number shown in the parentheses is 9, all arrays in VBScript
are zero-based, so this array actually contains 10 elements. In a zero-based
array, the number of array elements is always the number shown in parentheses
plus one. This kind of array is called a fixed-size array.
You assign data to each of the elements of the array using an index into the
array. Beginning at zero and ending at 9, data can be assigned to the elements
of an array as follows:
|
|
A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(9) = 55
|
|
Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:
|
|
. . .
SomeVariable = A(8)
. . .
|
|
Arrays aren't limited to a single dimension. You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. Multiple dimensions are declared by separating an array's size numbers in the parentheses with commas. In the following example, the user_Table variable is a two-dimensional array consisting of 5 rows and 10 columns:
|
|
In a two-dimensional array, the first number is always the number of rows;
the second number is the number of columns.
You can also declare an array whose size changes during the time your script is
running. This is called a dynamic array. The array is initially declared within
a procedure using either the Dim statement or using the ReDim statement.
However, for a dynamic array, no size or number of dimensions is placed inside
the parentheses. For example:
|
Dim MyArray()
ReDim AnotherArray()
|
|
To declare a dynamic array, use ReDim to determine the number of dimensions and the size of each dmension. In the following example, ReDim sets the initial size of the dynamic array to 20. A subsequent ReDim statement resizes the array to 25, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.
|
ReDim MyArray(20)
. . .
ReDim Preserve MyArray(25)
|
|
No limit how many number of times you can resize a dynamic array, but you should know that if you make an array smaller than it's current size then you lose the data from that particular variables
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
independent variables, types of variables, dependent variables,
php session variables, windows environment variables, controlled variables,
tube expanding procedure variables essential, linear equations in two variables,
confounding variables, demographic variables
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|