Solved

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

Posted on 2006-07-01
5
1,173 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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