xMRi
xMRi

Reputation: 15355

How to access an array directly that is returned from a function in VBScript

I have a function of a COM object that returns an Array.

This code works

Dim a
a = obj.Foo
MsgBox a(0)

This code says that there is no enumeration. And it is true.

MsgBox obj.Foo(0)

Also this didn't worked

MsgBox (obj.Foo)(0)

The problem is that the Foo function is a simple property.

Is there a syntax to directly access the data member?

Edit: Just to make clear that the function works correctly IsArray(obj.Foo) returns true!

Upvotes: 1

Views: 638

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

I believe the problem here is that variant array returned via COM needs to become a VB collection object, which does not happen immediately.

It happens when there is an intermediate variable, but it does not yet happen in the case of expression. In the latter case VBScript already sees what it treats as an object, but this object is a thin wrapper over variant array and it is not yet a collection object.

If you want a VBS one liner, this would work out:

Function TrueArray(ByVal Value)
  TrueArray = Value
End Function

'MsgBox obj.Foo(0) -- Object not a collection: 'obj.Foo'
MsgBox TrueArray(obj.Foo)(0)

You can also implement a collection on the other side of property implementation and return it instead of safe array (ATL C++ code would use CComEnum<IEnumVARIANT... helper, example).

I realize that your question is more about built-in scripting language capability that does the required conversion. Perhaps you could use some of the built-in functions to perform a similar trick to TrueArray above.

Upvotes: 1

Related Questions