Tuesday, November 4, 2014

VBScript Arrays

Arrays are sequence of memory, where collection of values can be stored. In VB we can store different type of values in a Array.


'Array can be Single Dimenstion OR Multi Dimension

'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.




'Declaration of an Array
'Declaration Type 1 : Mentioning the Size
Dim arr01(6) ''Declared with size of 6

' In VBScript Array can store any type of variable.

arr01(0) = "711"         'String
arr01(1) = "Raj Kumar"   'String
arr01(2) = 60000     'Number
arr01(3) = 4.25 'Decimal Number
arr01(4) = #11/04/1981#  'Date
arr01(5) = #08.45 AM#    'Time



data = "Data From Array arr01"& vbcrlf

For each i In arr01

data= data & i & vbcrlf

Next

MsgBox data




-------------------------------------------------------------

'Declaration Type 2 :


Dim arr02()  'Without Size OR dynamic-array

ArrSize = 2
ReDim arr02(ArrSize) ' ReDim is a statement to declare dynamic-array size and allocate or reallocate storage space.
arr02(0) = "745"
arr02(1) = "Salman"

data1 = "Data From Array arr02 after 1st declaration"& vbcrlf

For i=0 To UBound(arr02)

data1= data1 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next




ArrSize = 4                ' Rr Declare the size of the Array
ReDim arr02(ArrSize)   ' herre when we will reaalocate the Array the previous values of array will get clear. to Avoid this we can use PRESERVE
arr02(2) = 55000
arr02(3) = 5.24

data2 = vbcrlf& vbcrlf& "Data From Array arr02 after Re declare"& vbcrlf

For i=0 To UBound(arr02)

data2= data2 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next

MsgBox data1 & vbclrf & data2


ArrSize = 6                ' Rr Declare the size of the Array
ReDim PRESERVE arr02(ArrSize)   ' herre when we will reaalocate the Array the previous values of array will get clear. to Avoid this we can use PRESERVE
arr02(0) = "745"
arr02(1) = "Salman"
' we are not re assigning values to arr02(2) and (3) because we want to preserve it from existing
arr02(4) = #11/04/1976#
arr02(5) = #09.45 PM#

data3 =  vbcrlf& vbcrlf&  "Data From Array arr02 after Re declare with PRESERVE"& vbcrlf

For i=0 To UBound(arr02)

data3= data3 & "arr02("&i&"): "& arr02(i) & vbcrlf

Next

MsgBox data1 & vbclrf & data2 & vbclrf & data3





------------------------------

'Declaration Type 3 : using 'Array' Parameter
Dim arr03
arr03 = Array("One","Two","Three")

data3 =  "Data From Array arr03"& vbcrlf

For Each i  In arr03

data3= data3 & i & vbcrlf

Next

MsgBox data3







------------------------------------------------------------------------
'Array can be Single Dimenstion OR Multi Dimension 

'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.



Dim arr(2,3) ' Array which has 3 (0,1,2) rows and 4(0,1,2,3) columns

For x =0 To 2 

For y=0 To 3
arr(x,y)= "("& x &","& y & ")"
Next

Next


data =" Data From Array (2D)"& vbcrlf

For x =0 To 2 

For y=0 To 3
data = data& "  " & arr(x,y)
Next

data = data & vbcrlf

Next

MsgBox data