Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now