Rs Arrays
Last changed: -203.213.7.130

.
DeveloperRhinoScript
VersionRhino 4.0
SummaryDiscusses the differences between VBScript and JScript arrays.

Question

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?

Answer

The problem is that JScript arrays are objects but VBScript arrays are not.

A VBScript array is:

Whereas a JScript array is:

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:

  <SCRIPT LANGUAGE="VBSCRIPT">
  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: