如何通過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