Thursday, April 9, 2015

use to dynamically increase array size in VBScript

when we need to break the comma separate string into an array in vbscript, we can call the split function in VBScript,


STKReq="110,112,113,114,115"
stockReqArray=Split(STKReq, ",")

if we have already define an array with fixed length, in case i have a array which has size 10. but we did not the numbers in the string. we can first split the numbers into an number array, then use ReDim Preserve to dynamically increase the existing array and preserve all existing value by coping them to the new array.

"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)."

you can get more detail information from this MSDN link

ReDim Statement (Visual Basic)


the snippet code demonstrates the new array had been created and copied the exciting the value to the new array, then initialize the newly added elements with blank value, since they are null by default.

 ReDim Preserve stockReqArray(10)
 for i=stockReqArraySize to 10
      stockReqArray(i)=""
  next


No comments:

Post a Comment