Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scripting.Dictionary.Item property requires left hand value?

17 views
Skip to first unread message

Alex Blekhman

unread,
Sep 18, 2009, 5:48:28 AM9/18/09
to
Hello,

I encountered a strange problem with Scrioting.Dictionary object.
Consider the following code:

var d = new ActiveXObject("Scripting.Dictionary");
d.Item("one");

This code throws "Object doesn't support this property or method"
exception (Code: 800A01B6) on second line.

According to MSDN:

<quote>
If key is not found when changing an item, a new key is created
with the specified newitem. If key is not found when attempting to
return an existing item, a new key is created and its
corresponding item is left empty.
</quote>

So, the above code should be fine. New item with the key "one"
should be created and left empty. In order to avoid an exception I
must change the second line this:

var i = d.Item("one");

Now, when I assign newly created item to benign variable
everything works as intended. This behavior is reproducible both
on Windows XP (JScript 5.7) and Windows 7 (JScript 5.8).

The question is: Why "d.Item(key)" statement requires left hand
value in order to operate properly?

Thanks in advance
Alex

Alexander Mueller

unread,
Oct 17, 2009, 10:21:53 AM10/17/09
to
Alex Blekhman schrieb:

This behavior is not specific for the Scripting-Dictionary
but rather for any COM-objects property that takes args.

For example the same err is thrown for the FSO-Folders
Files-property:


var fs = new ActiveXObject("Scripting.FileSystemObject");
var f = fs.GetFile(WSH.ScriptFullName);
var d = f.ParentFolder
var f2 = d.Files(f.Name); //d.Files in an assignment
WSH.Echo (f.Name, f2.Name);

d.Files(f.Name); // now: object doesn't supp this meth-err thrown


As a shot into the blue, an explanation:

Properties in ActiveX/COM are marshaled/translated into

get_PropName(** out retval) //Getters
put_PropName(setterArg, ** out retval)//Setters

on some lower level.

If you don't have the assignment operator aka "=" on either the right
or left side of the expression, the runtime "thinks" the expression
is a Sub (void function in JS), since it doesn't return anything.
So it checks the namespace of the objects COM-Interface only for a
member with the *exact* name of the property, not prepending the
get_/put_ prefixes.
It will most likely not find the exact member's name, so it throws kind
of correctly "member not found".
If you have an assignment, the runtime knows it most check for both a
function with exact name and a property adding the prefix and thus it
doesn't fail to resolve the prop-Name to the underlying method-name
in the assignment.

just my 2ct,

Alex

Alex Blekhman

unread,
Oct 24, 2009, 4:30:53 PM10/24/09
to
"Alexander Mueller" wrote:
> As a shot into the blue, an explanation:
>
> Properties in ActiveX/COM are marshaled/translated into
>
> get_PropName(** out retval) //Getters
> put_PropName(setterArg, ** out retval)//Setters
>
> on some lower level.
> [...]

I was suspecting something like that. However, in the case of
d.Item("one") statement, I do supply a parameter, so there should
be no confusion that it is a put_Prop call. It is retval that
ignored. Apparently, I think that the requirement to have retval
all the time is a bit rigid. It is a script, after all. They could
make it more flexible.

Anyway, thanks for the answer.

Alex

0 new messages