VBScript Tutorials - Herong's Tutorial Examples - Version 5.24, by Dr. Herong Yang
Passing Arrays as Arguments
This section provides a tutorial example on how to pass an array as an arguments by reference to reverse positions of all elements in an array given as the argument.
As I mentioned earlier, arrays can also be passed as arguments. If an array is passed by reference, the procedure is working on the same array as the calling code. If an array is passed by value, the procedure is working on an independent copy of the original array in the calling code.
Here is an example code of using array as an argument, function_reverse_array.html:
<html> <body> <!-- function_reverse_array.html - Copyright (c) 2015, HerongYang.com, All Rights Reserved. --> <pre> <script language="vbscript"> document.writeln("") document.writeln("Test 1: Reversing a data literal") bOk = ReverseArray("Apple") aPets = Array("Bird", "Cat", "Dog", "Fish", "Rabbit") document.writeln("") document.writeln("Test 2: Reversing an array") document.writeln(" Before Sub: " & Join(aPets)) bOk = ReverseArray(aPets) document.writeln(" After Sub: " & Join(aPets)) Function ReverseArray(ByRef aList) If IsArray(aList) Then iMin = LBound(aList) iMax = UBound(aList) For i=iMin to iMax\2 j = iMax - (i-iMin) vTemp = aList(i) aList(i) = aList(j) aList(j) = vTemp Next ReverseArray = True Else document.writeln("Error: You are not giving an array.") ReverseArray = False End If End Function </script> </pre> </body> </html>
Here is the output:
Test 1: Reversing a data literal Error: You are not giving an array. Test 2: Reversing an array Before Sub: Bird Cat Dog Fish Rabbit After Sub: Rabbit Fish Dog Cat Bird
My "ReverseArray" function worked perfectly. You can take it as a utility function to your application.
Table of Contents