Thursday, March 29, 2018

arrays


Arrays
One dimensional arrays
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.

Dim arr1()    ‘without size
Dim arr1(5)  ‘with size of 5
Dim arr3       ‘with parameter
Arr3=array(“appple”,”orange”,”mango”)

·        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.
Example

Sub array_index()
Dim arr(3)
arr(0) = "sathis"
arr(1) = "kumar"
arr(2) = "siva"
arr(3) = "karthi"
MsgBox ("first array index:" & arr(0))
MsgBox ("second array index:" & arr(1))
MsgBox ("third array index:" & arr(2))
MsgBox ("fourth arry index:" & arr(3))
End Sub

Multi dimensional array
Arrays are not just limited to a single dimension, however, they can have a maximum of 60 dimensions. Two-dimensional arrays are the most commonly used ones.

example
In the following example, a multi-dimensional array is declared with 2rows and 3 columns.
Sub multi_array()
Dim arr(1,2)
Arr(0,0)=”sathis”
Arr(0,1)=”kumar”
Arr(0,2)=”siva”
Arr(1,0)=”12”
Arr(1,1)=”15”
Arr(1,2)=”32”
Msgbox (“array index 0,0 is:”&arr(0,0))
Msgbox(“array index1,2 is:”&arr(1,2))
End sub

Dynamic array
ReDim statement is used to declare dynamic-array variables and allocate or reallocate storage space.

Example
In the following example, an array has been redefined and then the values preserved when the existing size of the array is changed.

Sub dynamicarray1()
Dim a As Variant
i = 0
ReDim a(5)
a(0) = "apple"
a(1) = "orange"
a(2) = "mango"
ReDim Preserve a(10)
For i = 3 To 10
a(i) = i
Next
For i = 0 To UBound(a)
MsgBox a(i)
Next
End Sub


No comments:

Post a Comment

Data base function

Database function Daverage function      Averages the values in a field (column) of records in a list or database that match conditio...