Hi there
I'm trying to use the Scripting.Dictionary. However the item I'm adding to the dictionary is an array. An example is
Set objD = CreateObject("Scripting.Dictionary")
objD.Add "Key1", Array(1,2,3,4,5)
I know for sure the array always has 5 items.
In VBScript, I can access the item like this:
objD.item("Key1")(1) to objD.item("Key1")(5)
However how can I do it in JavaScript?
I think I can try
var myArray;
myArray = new VBArray(objD.item("Key1"))).toArray();
then I can access each item by
myArray[0] to myArray[4]
but is there a way to NOT make a myArray variable and just access the item individually?
I thought I could try
objD.item("Key1")[0] to objD.item("Key1")[4] but that won't work. It seems like I must do myArray.
Otherwise I think it's just some syntax that I'm missing. Hope someone can teach me about Javascript array.
Thanks
CC
phpnovice
03-27-2006, 05:58 PM
You don't need to use the Scripting.Dictionary to achieve those results. You can do this:
objD = new Object();
objD["Key1"] = new Array(1,2,3,4,5);
Then, you can access it as you would expect -- objD["Key1"][0] thru objD["Key1"][4] and you can iterate through your keys as follows:
for(key in objD) {
alert(objD[key][0]);
}
calgarychinese
03-27-2006, 06:05 PM
That's probably easier, but I'm doing code maintenance and obviously the original developer has used it all over the place in this asp file. I don't want to edit too much of his code (easily over 200 lines of code involving the dictionary).
I will however keep that in mind.
Until then, if I do have to use dictionary, what's the correct syntx to access the dictionary array item then?
Thanks
CC
netbuddy
03-27-2006, 07:10 PM
Yeah, the reason being would be that if the arrays were large, processing them would take longer than using the scripting dictionary which I tried a routine of mine on a very large file, it was like lightening, returned results in milliseconds as opposed to seconds using conventional arrays and loops.
you need
d = new ActiveXObject("Scripting.Dictionary");
to make the dictionary object 'd'
go here for more information http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/b4a7ddb3-2474-49ef-8540-8d67a747c8db.asp
phpnovice
03-27-2006, 07:23 PM
That's probably easier, but I'm doing code maintenance and obviously the original developer has used it all over the place in this asp file.
Oh, ASP. Are you sure the objD collection is available to client-side JavaScript? If it is being created by ASP/VBScript, then HTML/JavaScript will have no access to it.
phpnovice
03-27-2006, 07:28 PM
However how can I do it in JavaScript?
The following works fine in HTML/JavaScript -- however, are you doing this for an intranet? Because the following will require user-authorization unless you configure the browser to allow it.
objCollection = new ActiveXObject("Scripting.Dictionary");
objCollection.add("Key1", new Array(1, 2, 3, 4, 5));
alert(objCollection.item("Key1")[4]);
calgarychinese
03-27-2006, 07:53 PM
Hi netbuddy,
In my case the number will be small - maybe 5 to 10 items in the dictionary.
The challenge I have (I'm only given about 4 hours on this task so I can't really edit the code too much) is that the items are added to the dictionary via VBScript. And I'm in the event handler to process the dictionary in JavaScript.
It's an installed application so yes it'll become an intranet application, no security, etc. are of any concerns cos they're all taken care of.
Like I said, I've tried
var myArray;
myArray = new VBArray(objD.item("Key1"))).toArray();
and I can access each item by referencing
myArray[0] to myArray[4]
I'm just curious for my own sake, why, if I don't use myArray, I can't access the array items.
My original code was referencing
objD.item("Key1")[0] to objD.item("Key1")[4]
Out of desperation I split them into myArray and found the code to work. So the solution is there, I just want to understand the syntax of accessing Javascript array item such as the above.....
Thanks
CC
phpnovice
03-27-2006, 08:08 PM
Did you see my last post? I tested that and it works perfectly.
calgarychinese
03-28-2006, 10:27 AM
Hi phpinvoice
First to answer your question - yes it works, but it's because you defined the dictionary using javascript and accessed the dictionary array item with javascript.
I have finally made an example (as below). This should illustrate my frustration.
Basically, I have instantiated 2 dictionaries, one using VBScript and one using Javascript. Both contains 2 entries, and the first entry contains an array of 5 letters (from a to e), and the second entry contains an array of 5 letters (from f to j).
I have 4 buttons, all try to access the 2nd element of the 2nd item. It should return g.
1. first button: access the VB-defined dictionary using VBscript, return g.
2. second button: access the VB-defined dictionary using Javascript, return undefined.
3. third button: access the JS-defined dictionary using VBScript, to my surprise, gives me a script error "Object doesn't support this property or method: loDict2.Item(...)"
4. fourth button: access the JS-defined dictionary using Javascript, return g.
So, if you define the dictionary in one langauge and access the array item using the same langauge (as in the case of 1 and 4), then you get what you expect.
The part I don't understand is 2 and 3. Why in one case it returns undefined, and in another case it gives me an error.
The code may be a bit long, but if you save it as an asp file and put it in your web server and try it, you'll see what I'm trying to get at.
Hope to get some help to understand the syntax better. Thanks.
CC
<html>
<body>
<script language=vbscript>
dim loDict1
dim loDict2
sub window_onload()
call vb_onload()
call js_onload()
end sub
</script>
<script language=vbscript>
sub vb_onload()
set loDict1 = createobject("Scripting.Dictionary")
loDict1.Add 1, Array("a","b","c","d","e")
loDict1.Add 2, Array("f","g","h","i","j")
end sub
sub getVBVB()
msgbox loDict1.Item(2)(1)
end sub
sub getJSVB()
msgbox loDict2.Item(2)(1)
end Sub
</script>
<script language=javascript>
function js_onload()
{
loDict2 = new ActiveXObject("Scripting.Dictionary");
loDict2.add(1, new Array("a","b","c","d","e"));
loDict2.add(2, new Array("f","g","h","i","j"));
}
function getVBJS()
{
alert (loDict1.item(2)[1]);
}
function getJSJS()
{
alert (loDict2.item(2)[1]);
}
</script>
2 dictionaries are defined, loDict1 and loDict2, both have 2 entries, keyed by 1 and 2.
<br>1 contains an array of 5 elements, a, b, c, d, e,<br>
2 contains an array of 5 elements, f, g, h, i, j.
<br>All 4 buttons below try to access the 2nd element of the second item (should return g)<br>
<input type=button value='VBArray VBAccess should get g' name=btnVBVB onclick=getVBVB()><br>
<input type=button value='VBArray JSAccess should get g' name=btnVBJS onclick=getVBJS()><br>
<input type=button value='JSArray VBAccess should get g' name=btnJSVB onclick=getJSVB()><br>
<input type=button value='JSArray JSAccess should get g' name=btnJSJS onclick=getJSJS()><br>
</body>
</html>
phpnovice
03-28-2006, 11:23 AM
Hi phpinvoice
yes it works, but it's because you defined the dictionary using javascript and accessed the dictionary array item with javascript.
Well, you asked your question in a JavaScript forum -- what did you expect me to use? ;) At any rate... I'll take a closer look (when I get home this evening) at why the following doesn't work as expected -- it may be that CreateObject() creates an object type (i.e., not an ActiveXObject) that JavaScript cannot access:
However, another question is... Why not use VBScript for everything -- skip JScript altogether?
calgarychinese
03-28-2006, 11:43 AM
Hi
THanks for your quick response.. Once again, it's because the code was written by someone else and I wasn't supposed to "touch" his stuff, only the part where there was a bug.
Once again, if I split the items into an array, then it seems that my VB-defined dictionary can be accessed by JavaScript. I just HAVE to split the item into a new array variable, and I don't understand why.
CC
phpnovice
03-28-2006, 01:58 PM
I do seem to remember a "problem" like this when trying to store an array in an ASP Session variable -- with basically the same resolution (storing the array as a string and splitting it into an actual array when needed).
phpnovice
03-28-2006, 06:54 PM
...
phpnovice
03-28-2006, 07:09 PM
Actually... I think you've already got the answer and didn't know it... JavaScript cannot access VBScript arrays because VBScript arrays do not always start with index 0 and each dimension of a VBScript array can start with a different index. Thus, the VBScript array must be converted to something JavaScript can handle.
VBArray Object
Provides access to Visual Basic safe arrays.
Remarks:
VBArrays are read-only, and cannot be created directly. The safeArray argument must have obtained a VBArray value before being passed to the VBArray constructor. This can only be done by retrieving the value from an existing ActiveX or other object.
VBArrays can have multiple dimensions. The indices of each dimension can be different. The dimensions method retrieves the number of dimensions in the array; the lbound and ubound methods retrieve the range of indices used by each dimension.
VBArray Object (http://msdn.microsoft.com/library/en-us/script56/html/f0b767f1-ea8a-4726-962b-2708d4742518.asp)
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.