Discussion:
create VBArray from JS-Array
(too old to reply)
Christoph Basedau
2005-11-30 17:11:13 UTC
Permalink
Hi

if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
As far as i can see, the VBArray()-constructor mandatorily needs
an existing safeArray, which cannot be empty or undefined, but there
is no JScript-solution for creating one.

Is there a way to create a 'dummy' VBArray and redim it and
copy JSArray-items one by one into it, kind of

//dummycode

var jsArr = [1,2,3,4,5,6,7,8,9];
var vbArr = new VBArray(emptySA);

vaArr.redim (jsArr.length-1);
for (var i=0; i<jsArr.length; i++)
vbArr[i] = jsArr[i];


I know that this won't work, because there is no redim-method
and also no direct assignment to a VBArray-item, like:
vbArr[i] = jsArr[i];
however this is what i want to achieve.
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Evertjan.
2005-11-30 18:35:58 UTC
Permalink
Christoph Basedau wrote on 30 nov 2005 in
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
var str = arr.join('/')
...
arr = split(str,"/")

?
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)
Christoph Basedau
2005-12-01 20:30:48 UTC
Permalink
Post by Evertjan.
Christoph Basedau wrote on 30 nov 2005 in
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
var str = arr.join('/')
...
arr = split(str,"/")
From the VBS-side it's okay. I also noticed that
VBS converts a JScript-array anyway into a comma-delimited
string

<job>
<script language="JScript">
function getArray() {return [1,2,3,4,5,6];}
</script>
<script language="VBScript">
a = getArray()
WScript.Echo TypeName(a), a
</script>
</job>

The problem is that i want a wsf-library-function that converts
hex-strings into numeric arrays for the given language (for StdRegProv.SetBinaryValue).
The caller shall not have anything to do with conversion-issue, simply call it:
vArray = getArrayFromHexString("0A0B0FCC")
Think the dict.Items()-Collection finally is a good solution.
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Evertjan.
2005-12-01 23:31:35 UTC
Permalink
Christoph Basedau wrote on 01 dec 2005 in
Post by Christoph Basedau
Post by Evertjan.
Christoph Basedau wrote on 30 nov 2005 in
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
var str = arr.join('/')
...
arr = split(str,"/")
From the VBS-side it's okay. I also noticed that
VBS converts a JScript-array anyway into a comma-delimited
string
I doubt that.
This is only the displaying, not the string conversion, IMHO.
Post by Christoph Basedau
<job>
<script language="JScript">
function getArray() {return [1,2,3,4,5,6];}
</script>
<script language="VBScript">
a = getArray()
WScript.Echo TypeName(a), a
</script>
</job>
The problem is that i want a wsf-library-function that converts
hex-strings into numeric arrays for the given language (for
StdRegProv.SetBinaryValue). The caller shall not have anything to do
vArray = getArrayFromHexString("0A0B0FCC")
Think the dict.Items()-Collection finally is a good solution.
I cannot follow you here, it seems you have a new question.

In jscript you can change any string
into an array of it'ds characters by:

s = "0A0B0FCC hello"

a = s.split('')
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)
Christoph Basedau
2005-12-02 00:45:25 UTC
Permalink
Post by Evertjan.
Post by Christoph Basedau
Post by Evertjan.
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
var str = arr.join('/')
arr = split(str,"/")
From the VBS-side it's okay. I also noticed that
VBS converts a JScript-array anyway into a comma-delimited
string
I doubt that.
This is only the displaying, not the string conversion, IMHO.
you're right that WScript.Echo can display arrays, so the output looks like string,
and also that the returned jsArr itself at first is not a string, but the
JSArray itself, which is a COM-Object.
But as soon as it is assigned to a vbs-variable, it 'becomes' a string
indicated by TypeName a/o VarType, which return 'String' a/o vbString.

Since vbs can't handle js-arrays, it could either raise an error or
try to convert it to something known and because every JS-Object or base-typ is
convertable to string, vbs afaics calls toString() for each item and then
joins them to a list.
You can see that toString() is called by in a self-defined function-prototype
that implements toString() or the '[object Object]'-string for native
JS-Objects.
If one item of the array is a COM-Object that has no toString()-method and
no default-prop, VBS can't convert the Array and an error occurs.


<job><script language="JScript">
function f() {this.toString = function(){return "F-func";}}
var fs = new ActiveXObject("Scripting.FileSystemObject");
function getArray() {return [new f(), {a:'hello'},/abc/,fs.GetSpecialFolder(2)];}
function getArray2() {return [fs];}
</script>
<script language="VBScript">
Dim a, b
a = getArray()
WScript.Echo a '->toString() for all items
WScript.Echo "returned type =", TypeName(getArray())
WScript.Echo "type after assignment =", TypeName(a)
b = getArray2() 'error cause 'fs' has no toString
</script>
</job>
Post by Evertjan.
Post by Christoph Basedau
The problem is that .. the caller shall not have anything to do
vArray = getArrayFromHexString("0A0B0FCC")
Think the dict.Items()-Collection finally is a good solution.
I cannot follow you here, it seems you have a new question.
In jscript you can change any string
It doesn't matter, i only wanted to say why i don't want the VBS-client to
do the conversion.
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Martin Honnen
2005-11-30 18:46:34 UTC
Permalink
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
I don't know any method or function for that, and the documentation for
toArray (the other way round)
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthtoArray.asp>
says:
"There is currently no way to convert a JScript array into a VBArray."

You might want to seach the blog of Eric Lippert
<http://blogs.msdn.com/ericlippert/>, whether he has said anything about
doing that. One entry is this:
<http://blogs.msdn.com/ericlippert/archive/2003/09/22/53061.aspx>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Christoph Basedau
2005-12-01 20:33:20 UTC
Permalink
30.11.2005 19:46, Martin Honnen schrieb:

Hi Martin
Post by Martin Honnen
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
I don't know any method or function for that, and the documentation for
toArray (the other way round)
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthtoArray.asp>
"There is currently no way to convert a JScript array into a VBArray."
Okay, a definitive answer.
Post by Martin Honnen
You might want to seach the blog of Eric Lippert
<http://blogs.msdn.com/ericlippert/>, whether he has said anything about
<http://blogs.msdn.com/ericlippert/archive/2003/09/22/53061.aspx>
Seems that he and his colleague wrote some code, but there wasn't much demand among
scripters for implementing a 'toVBArray()-method. So they let it be. too bad.
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Steve Fulton
2005-11-30 21:49:16 UTC
Permalink
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
The Scripting.Dictionary ActiveX object (rarely used in JScript because
the native Object object is more handy) has an Items method that returns
a SafeArray.

var jsArr = [1,2,3,4,5,6,7,8,9];
var wsDic = new ActiveXObject("Scripting.Dictionary");
for (var i=0; i<jsArr.length; i++) wsDic.Add(i, jsArr[i]);
var vbArr = wsDic.Items();

Dictionary Object
http://msdn.microsoft.com/library/en-us/script56/html/jsobjdictionary.asp

Items Method
http://msdn.microsoft.com/library/en-us/script56/html/jsmthitems.asp

Download Windows Script Documentation
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9
--
Steve

Do not be too moral. You may cheat yourself out of much of life.
So aim above morality. Be not simply good; be good for something.
-Henry David Thoreau
Christoph Basedau
2005-12-01 20:24:45 UTC
Permalink
Post by Steve Fulton
Post by Christoph Basedau
if i have a JScript-Array, is there a way to convert it into
VBArray-Object?
The Scripting.Dictionary ActiveX object (rarely used in JScript because
the native Object object is more handy) has an Items method that returns
a SafeArray.
var jsArr = [1,2,3,4,5,6,7,8,9];
var wsDic = new ActiveXObject("Scripting.Dictionary");
for (var i=0; i<jsArr.length; i++) wsDic.Add(i, jsArr[i]);
var vbArr = wsDic.Items();
Thanks, that works great.
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Loading...