Link to home
Start Free TrialLog in
Avatar of ieg
iegFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do you pass a VBscript array into c# Class method call

I have written a c# class that I use in a VbScript running in windows scripting Host to automate an external application.
It works well and passing simple variables like strings as parameters to methods is Ok

I now want to build an array in Vbscript and pass the array as a parameter to the method

'''''''''''''''''''''''''''''''''''''''''''''''''
e.g. VbScript code
dim strArr(2)
strArr(0) = "01001"
strArr(1) = "01002"

dim obj
set obj = CreateObject("mytest.mytest")
ok = obj.SetMultipleValues("StockCode", strArr)
set obj = nothing
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The c# code is
public bool SetMultipleValues(string strParamName, string[] strValueArr)
{
  //some code that loops through array
}


The error I get is that VbScript says "Invalid procedure call or argument ...."

experimentation indicates that it is the array that is the problem.
I suspect that the Vbscript array is not really an array as c# would expect

Can anyone help?

Thanks

Andy



ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ieg

ASKER

Thanks bruintje,
using TypeName in Vbscript shows me that I have a variant array.
I don't seem to be able to get this into the c# code because I get a Windows scripting Host error
"Variable uses an Automation type not supported in Vbscript"

Am I right in assuming that Vbscript can't pass the array?
In the c# code i have used system.object to try and capture the variant (see below) but I think this is looking more like a restriction in Vbscript than a c# issue.
public bool SetMultipleValues(string strParamName, object strValueArr)

Any thoughts?

Andy

Avatar of aaronfeng
aaronfeng

In your C# code, you can try to take in an object instead string[] then try to cast it to string[].  You might have to play around with the type you need to pass in and cast to.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
>>public bool SetMultipleValues(string strParamName, object strValueArr)

yes that's what i meant with option 2, but i'm not sure how a cast to string[] would play out, can you cast an object to string[] in one go?

that's why option 1 using a long string that got split on the C# side is an easier option because you can almost be sure it will work as expected :)
Avatar of ieg

ASKER

Thanks Brian - I am going to go with your option 1 - it works well.

Andy