What is an Array?
We know very well that a variable is a container to
store a value. Sometimes, developers are in a position
to hold more than one value in a single variable at
a time. When a series of values is stored in a single
variable, then it is known as an array variable.
Array Declaration
Arrays are declared the same way a variable has been
declared except that the declaration of an array
variable uses parenthesis. In the following example,
the size of the array is mentioned in the brackets.
'Method 1 : Using Dim
Dim arr1() 'Without Size
'Method 2 : Mentioning the Size
Dim arr2(5) 'Declared with size of 5
'Method 3 : using 'Array' Parameter
Dim arr3
arr3 = Array("apple","Orange","Grapes")
• Although, the Array size is indicated as 5, it can hold 6 values as array index starts from ZERO.
• Array Index Cannot be Negative.
• VBScript Arrays can store any type of variable in an array.
Hence, an array can store an integer, string or characters in a single array variable.
Assigning Values to an Array
The values are assigned to the array by specifying array index
value against each one of the values to be assigned. It can be a string.
Example
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim arr(5)
arr(0) = "1" 'Number as String
arr(1) = "VBScript" 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
document.write("Value stored in Array index 0 : " & arr(0) & "<br />")
document.write("Value stored in Array index 1 : " & arr(1) & "<br />")
document.write("Value stored in Array index 2 : " & arr(2) & "<br />")
document.write("Value stored in Array index 3 : " & arr(3) & "<br />")
document.write("Value stored in Array index 4 : " & arr(4) & "<br />")
document.write("Value stored in Array index 5 : " & arr(5) & "<br />")
</script>
</body>
</html>
When the above code is saved as .HTML and executed in
Internet Explorer, it produces the following result:
Value stored in Array index 0 : 1
Value stored in Array index 1 : VBScript
Value stored in Array index 2 : 100
Value stored in Array index 3 : 2.45
Value stored in Array index 4 : 7/10/2013
Value stored in Array index 5 : 12:45:00 PM
No comments:
Post a Comment