Solved

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

Posted on 2006-07-01
5
1,178 Views
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?

Thanks

Andy



0
Comment
Question by:ieg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
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

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:ieg
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?

Andy

0
 
LVL 5

Expert Comment

by:aaronfeng
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.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 44

Expert Comment

by:bruintje
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 :)
0
 

Author Comment

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

Andy
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

751 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