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

Posted on 2006-07-01
Last Modified: 2008-01-09
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?



Question by:ieg
  • 2
  • 2
LVL 44

Accepted Solution

bruintje earned 500 total points
ID: 17023258
Hi ieg,

this is only my assumption but you are correct about the difference in array types

in VBS you have no real types though you name them str its all implicit a variant type thus the array can be anything from C# viewpoint with simple types an implicit conversion goes ok but with complex types not

you could look in the debugger what comes in the C# code when you pass it from VBS

you could look at these 2 possible ways out

1. using a seperated string that you use a split on in C# to get the individual parts
2. using a typecast to a string array in C# but i'm not so sure if its safer then option 1

share what you know, learn what you don't

Author Comment

ID: 17023693
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?



Expert Comment

ID: 17024051
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.


LVL 44

Expert Comment

ID: 17025847
>>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 :)

Author Comment

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


Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question