Site Tools


This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:arrays [2015/09/14] (current)
Line 1: Line 1:
 +====== VBScript and JScript Arrays ======
 +> **Developer:​** [[developer:​rhinoscript|RhinoScript]]
 +> **Summary:​** //Discusses the differences between VBScript and JScript arrays.//
 +I am trying to return array from within an HTML dialog box, that uses JScript, using RhinoScript'​s HtmlBox function, but I always get an error. What's up with that?
 +The problem is that JScript arrays are objects but VBScript arrays are not.
 +A VBScript array is:
 +  * multi-dimensional
 +  * indexed by integer tuples
 +  * dense
 +  * not an object
 +Whereas a JScript array is:
 +  * one dimensional.
 +  * associative;​ JScript arrays are indexed by strings. ​ Numeric indices are actually converted to strings internally.
 +  * sparse: arr[1] = 123; arr[1000000] = 456; gives you a two-member array, not a million-member array.
 +  * an object with properties and methods.
 +As you might expect, VBScript and JScript arrays have completely different implementations behind the scenes. ​ A JScript array is basically just a simple extension of the existing JScript object infrastructure,​ which is implemented as a hash table. ​ A VBScript array is implemented using the SAFEARRAY data structure, which is just a structure wrapped around a standard "chunk of memory"​ C-style array.
 +Since all the COM objects in the world expect VBScript-style arrays and not JScript-style arrays, making JScript interoperate with COM is not always easy.
 +=====More information=====
 +Occasionally a VBScript routine will pass an array as one of its parameters or as its return value. You can call such a routine from JScript, but must convert the VBScript array into a usable JScript array. To do so, create a **VBArray** object in your JScript function and convert it to a JScript array using **toArray**. Note, because JScript does not support multidimensional arrays, if the original VBScript array was multidimensional,​ the **toArray** method converts it to a single-dimension JScript array.
 +The following example includes a VBScript script that creates an array and a JScript script that illustrates how to get and use the array:
 +<code vb>
 + ​Function makeArrayVB()
 +   '​ Creates a VBScript array
 +   dim anArray(1,​1)
 +   ​anArray(0,​0) = "​0,​0"​
 +   ​anArray(0,​1) = "​0,​1"​
 +   ​anArray(1,​0) = "​1,​0"​
 +   ​anArray(1,​1) = "​1,​1"​
 +   ​makeArrayVB = anArray
 + End Function
 + <​SCRIPT LANGUAGE="​JavaScript">​
 + // Accesses a VBScript array within a JScript script
 + ​function getVBArray()
 + {
 +   var arrayObj;
 +   var jsArray;
 +   ​arrayObj = makeArrayVB();​
 +   ​jsArray = VBArray(arrayObj).toArray();​
 +   ​alert("​VBScript array length = " + jsArray.length);​
 +   // Displays the contents of the array
 +   ​for(i=1;​i<​=jsArray.length;​i++)
 +   {
 +     ​alert(jsArray[i-1]);​
 +   }
 + }
 + </​SCRIPT>​
 +Unfortunately,​ the reverse is not possible: You cannot convert a JScript array into a VBScript one. If you're faced with a JScript routine that passes an array, you have these options:
 +  * Rewrite the JScript routine in VBScript.
 +  * If possible, alter the JScript array to pass a different structure, such as a delimited string, that VBScript can handle. For example, you can use the **toString** function to turn an array into a comma-delimited string. Then in VBScript, you can use the **Split** function to peel off individual elements. Obviously, this is not a practical solution in many cases, but can sometimes be worthwhile.
 +{{tag>​Developer RhinoScript}}
developer/scriptsamples/arrays.txt ยท Last modified: 2015/09/14 (external edit)