C# COM array issues when accessed through vbscript

G

Guest

Greetings,

I'm trying to access C# COM object from a VBscript, where C# will
modify the reference parameter to an array.

I found simple reference parameter datatypes (i.e. int, string) yield
to expected result, however, when I try to create an array of these
primitives, I encounter strange behaviors!

For example, given the below C# class library COM
(Signed, and regasm /codebase), you'll see that it has one method
in which it simply allocates and initializes an array of int[].

In VBscript, the reference parameter of testMethod ( refParam )
will yield to an array of type Int with array boundaries[0..2].
However, weather I attempt to print or check the type
of the indexed array element I get a type mismatch error!

Do you know how I can access index elements, or what is wrong with
this logic?

Thanks in advance


//////////////////////////////////////////////////////
// C# Class Library
//
using System;

namespace testSpace {

public class testClass {

public void testMethod( ref object o ) {

o = new int[3] {20, 21, 22};
System.Console.WriteLine( "debug Val=" + ((int[])o)[0] + "\n");
}
}
} // End Of C# Code
//////////////////////////////////////////////////////


''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' VBscript
'
set o = createObject( "testSpace.testClass" )

dim refParam
call o.testMethod ( refParam )

''''''''''''''''''''
' Reference: http://www.w3schools.com/vbscript/func_vartype.asp
' Next line outputs: "Type=8195, bound( 0, 2)"
wscript.echo "Type=" _
& vartype(refParam) _
& ", bound( " _
& lbound(refParam) _
& ", " & ubound( refParam ) & ")"

''''''''''''''''''''
' Next line outputs: "Microsoft VBScript runtime error: Type mismatch"
msgbox vartype (refParam(0))

'' End Of VBscript Code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
M

Mattias Sjögren

Do you know how I can access index elements, or what is wrong with
this logic?

I believe the problem is that the script code only supports Variant
arrays (object array on the C# side). Try changing

o = new int[3] {20, 21, 22};

to

o = new object[3] {20, 21, 22};


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top