What is String [*] and how do I create it?

I am currently in an unsuccessful situation where you are calling a VBA macro from C # through COM Interop. The macro returns an array of strings and looks something like this:

Public Function Foo() As String()

which then I try to apply to an array of strings in C # as follows:

var result = (string[])xlApp.Run("Foo", ... missing args ...)

which then leads to a runtime error:

Unable to pass object of type 'System.String [*]' for input 'System.String []'.

Does anyone know what String [*] is and you have an idea on how to pass it to a C # [] string? This selection works in a simple test case that I created. The only difference that I see is that in the simple case, my array in VBA has a string of type (0 to 5), while in a more complex case it is a string of type (1 to 6). Could this be the reason, and if so, my VBA is pretty rusty - how can I fix it?

Thanks a lot
John

+3
source share
2 answers

Your code was

var result = (string[])xlApp.Run("Foo", ... missing args ...);

As you pointed out, there is a different behavior with an array based on 1, with a zero array in VB, which your code really works.

You can get data like this

var result = (Array)xlApp.Run("Foo", ... missing args ...);

var stringsResult = result.Cast<string>[];

IEnumerable

+1

.NET ( , 0), :

Array arr = Array.CreateInstance(typeof(string), new int[] { 5 }, new int[] {1});

[] - ( , , , , , ):

string[] x = new string[5];
Array.Copy(arr, 1, x, 0, 5);

: :

string[] x = new string[arr.GetLength(0)];
Array.Copy(arr, arr.GetLowerBound(0), x, 0, arr.GetLength(0));

Edit2: != 0 , , , , , , .

Edit3: , djna. [] , , [], lowerBound!= 0:

Array arr = (Array)xlApp.Run("Foo", ... missing args ...); //Here I assume that .Run() method is returning object, or something that has to be casted to Array.
string[] result = new string[arr.GetLength(0)];
Array.Copy(arr, arr.GetLowerBound(0), result, 0, arr.GetLength(0)); // Here data is copied, but only references in this scenario
+5

Source: https://habr.com/ru/post/1716901/


All Articles