如何通过COM Interop将VB6字符串数组[[假设,s = Array(“a”,“b”,“c”,“d”)]传递给C#.Net ?将字符串数组从VB6传递到C#.net
我试图实现传递C#字符串数组到VB和VB字符串数组到C#如下C# - > VB工作正常,但其他方式(VB => C#)给出编译错误称为“函数或接口标记为受限,或者该功能使用Visual Basic不支持的自动化类型“。我下面
C#
public interface ITest
{
string[] GetArray();
void SetArray(string[] arrayVal);
}
public class Test : ITest
{
string[] ITest.GetArray() { //Working fine
string[] stringArray = { "red ", "yellow", "blue" };
return stringArray;
}
}
void ITest.SetArray(string[] arrayVal) //Giving an issue
{
string[] stringArray1 = arrayVal;
}
VB
Dim str As Variant
Debug.Print ".NET server returned: "
For Each str In dotNETServer.GetArray 'dotNETServer=TestServer.Test
Debug.Print str
Next
Dim arr(3) As String
arr(1) = "Pahee"
arr(2) = "Tharani"
arr(3) = "Rathan"
dotNETServer.SetArray (arr) 'This one causing the compile error which I mentioned earlier
更新代码: ::::::
We need to pass the array as reference in C#。在界面和方法中更改它
void SetArray(ref string[] arrayVal); //ref added
我喜欢JSON方法:http://stackoverflow.com/questions/15649696/how-can-i-pass- a-collection-of-objects-from-vb6-to-net –
只需在VB6中使用字符串数组,Dim arr(42)As String即可。如果您有Option Base 0有效,它会自动封送到字符串[]。如果你想出于某种原因使用Variant,那么你必须在C#中使用* object *并进行强制转换。 –
@HansPassant,非常感谢。任何想法我的问题发送VB6字符串数组到C#? – RobinAtTech