【发布时间】:2014-05-07 01:56:15
【问题描述】:
如何通过 COM Interop 将 VB6 字符串数组 [Assume, s =Array("a", "b", "c", "d")] 传递给 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 方法:stackoverflow.com/questions/15649696/…
-
在VB6中使用字符串数组,Dim arr(42) As String。如果您有 Option Base 0 有效,它会自动编组为 string[]。如果您出于某种原因想使用 Variant 进行操作,那么您必须在 C# 中使用 object 并进行强制转换。
-
@HansPassant,非常感谢。对我的问题有任何想法将 VB6 字符串数组发送到 C#?
-
我在谈论编组字符串 from vb6 to C#。这意味着“发送”。
标签: c# com vb6 interop com-interop