www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons VBScript Arrays Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14

An array in VBScript is a special type of variable that allows us to store multiple values against a single variable name.

Suppose we need to store yearly temperatures of a particular place. We can use normal variables to do so, but the resultant code would be very hard to manage and maintain. In such situations VBScript arrays come to our help.

Arrays can be visualized as a stack of elements, where each element has an index number and a value. The following table resembles an array:

Array
0 Mango
1 Apple
2 Orange

The numbers on the left are indices and on the right are the values. Note that VBScript arrays start their numbering at zero.

The syntax for creating a VBScript array follows:

Dim fruits(3)
fruits(0) = "Mango"
fruits(1) = "Apple"
fruits(2) = "Orange"

The first line of the code above declares an array named fruits having 3 elements. The susequent lines store values to the array indices.

We can access data in an array by referring to the name of the array and the element's index number. The syntax for accessing array elements follows:

Dim myFruit
myFruit = fruits(1)

The above code assigns the value at index 1, second element, of the fruits array to myFruit.

The following example declares an array, assigns values to its indices and then outputs the second element of the array:
<script type="text/vbscript">
Dim fruits(3)
fruits(0) = "Mango"
fruits(1) = "Apple"
fruits(2) = "Orange"
document.write(fruits(1))
</script>

The output would be:

Apple


We can very easily modify the contents of a VBScript array. Assigning a new value to an array index, discards the old value and updates the index with the new value.

Here is an example:

<script type="text/vbscript">
Dim fruits(3)
fruits(0) = "Mango"
fruits(1) = "Apple"
fruits(2) = "Orange"
fruits(1) = "Banana"
document.write(fruits(1))
</script>

The output would be:

Banana

Next >>> Lesson No. 8: VBScript If Statement






Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.