11/4/2015
- Arrays have lower and upper limit. Lower limit in Array is 0 and it called as base.
Syntax of declaring Array:
There are 3 methods of declaring an array:
Source: http://www.tutorialspoint.com/vbscript/vbscript_arrays.htm
'Method 1 : Using Dim
Dynamic Array:
Here size of an array could be changed during the run time.
Sub arraysize()
Dim arrTestArray()
End Sub
- Option Base statement:
Option Base statement in Array allows user to specify base index.
Ex:
Option Base 1
Dim Weekday(7) as String
Note: Here the Weekday is array, its lower/base value starts with 1.
- To keyword is used to declare min and max index numbers for an array.
dim array1(1 to 10) as Integer
- Boundaries of an array could be found using UBound and LBound.
- 3 Types of Arrays are:
Single, Multi and Dynamic array.
Dynamic arrays can be resized at any time. They help to manage memory efficiently. The ReDim statement is used to specify the bounds of array at run time.
Dim arr1() as Int
Redim arr1(2.3.1) --
Explain more about Redim:
Website:
http://windowsitpro.com/article/vbscript/understanding-vbscript-arrays-5627
The website has complete detials of Array.
- Arrays have lower and upper limit. Lower limit in Array is 0 and it called as base.
Syntax of declaring Array:
There are 3 methods of declaring an array:
Source: http://www.tutorialspoint.com/vbscript/vbscript_arrays.htm
'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")
Method 4:dim arr3(0 to 5)
Dynamic Array:
Here size of an array could be changed during the run time.
Sub arraysize()
Dim arrTestArray()
intSize = 0
'ReDim - to redeclare the size of an array
'Preserve - to
ReDim Preserve arrTestArray(intSize)
arrTestArray(intSize) = 10
ReDim Preserve arrTestArray(intSize + 1)
arrTestArray(intSize + 1) = 20
MsgBox UBound(arrTestArray)
MsgBox LBound(arrTestArray)
For i = LBound(arrTestArray) To UBound(arrTestArray)
MsgBox arrTestArray(i)
Next
End Sub
- Option Base statement:
Option Base statement in Array allows user to specify base index.
Ex:
Option Base 1
Dim Weekday(7) as String
Note: Here the Weekday is array, its lower/base value starts with 1.
- To keyword is used to declare min and max index numbers for an array.
dim array1(1 to 10) as Integer
- Boundaries of an array could be found using UBound and LBound.
- 3 Types of Arrays are:
Single, Multi and Dynamic array.
Dynamic arrays can be resized at any time. They help to manage memory efficiently. The ReDim statement is used to specify the bounds of array at run time.
Dim arr1() as Int
Redim arr1(2.3.1) --
Explain more about Redim:
- Can be Resized using Redim
- Redim cannot be used to change the data type of an array
- Arrays must be declared without initial size while using Redim statement
- Redim cannot be used to change the dimensions
- Preserve keyword in the Redim statement is used to preserve the existing values
Redim Preserve customer(20)
Website:
http://windowsitpro.com/article/vbscript/understanding-vbscript-arrays-5627
The website has complete detials of Array.
Redim:
Source: https://msdn.microsoft.com/en-us/library/w8k3cys2.aspx
Redim is used to redeclare the size of an array.
You can use the ReDim statement to change the size of one or more dimensions of an array that has already been declared. If you have a large array and you no longer need some of its elements, ReDim can free up memory by reducing the array size. On the other hand, if your array needs more elements, ReDim can add them.
The ReDim statement is intended only for arrays. It's not valid on scalars (variables that contain only a single value), collections, or structures. Note that if you declare a variable to be of type Array, the ReDim statement doesn't have sufficient type information to create the new array.
Preserve:
Helps to preserve the values in an array if the size is being reduced. Actually when Redim is used vb script will create new array with specified size, inorder to make use of values in the existing array use Preserve, else it will not copy the values but it does create new array will all default values in it.
Examples:
Dim intArray(10, 10, 10) As Integer ReDim Preserve intArray(10, 10, 20) ReDim Preserve intArray(10, 10, 15) ReDim intArray(10, 10, 10)
The Dim statement creates a new array with three dimensions. Each dimension is declared with a bound of 10, so the array index for each dimension can range from 0 through 10. In the following discussion, the three dimensions are referred to as layer, row, and column.
The first ReDim creates a new array which replaces the existing array in variable intArray. ReDim copies all the elements from the existing array into the new array. It also adds 10 more columns to the end of every row in every layer and initializes the elements in these new columns to 0 (the default value of Integer, which is the element type of the array).
The second ReDim creates another new array and copies all the elements that fit. However, five columns are lost from the end of every row in every layer. This is not a problem if you have finished using these columns. Reducing the size of a large array can free up memory that you no longer need.
The third ReDim creates another new array and removes another five columns from the end of every row in every layer. This time it does not copy any existing elements. This statement reverts the array to its original size. Because the statement doesn't include the Preserve modifier, it sets all array elements to their original default values.
How to check size of an array?
ReplyDeleteDim a(7) 'as String
Deletea(0)="hi"
a(1)="hi1"
msgbox UBOUND(a)
UBound(arr) -- gives the highest subscript, but sometimes you need to know the index till where exist in the array. For that use For loop(for going through array) and if condition to exit for loop when value is "". By doing this you will get the exact size of array.
DeleteThis comment has been removed by the author.
ReplyDelete'Dim a(7) as String
ReplyDeletea(0)="1"
a(1)="2"
'msgbox UBOUND(a)
msgbox "hi"