将字符串数组从VB6传递到C#.net [英] Passing string array from VB6 to C#.net

查看:370
本文介绍了将字符串数组从VB6传递到C#.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何传递VB6字符串数组[假设,s = Array(a,b,c,d)]到C#.Net通过COM Interop?

How to pass a VB6 string array [Assume, s =Array("a", "b", "c", "d")] to C#.Net through COM Interop?

我试图实现传递C#字符串数组到VB和VB字符串数组到C#如下C# - > VB工作正常但其他方式(VB => C#)给出编译错误称为函数或标记为受限的接口,或者该函数使用了在可视基本中不支持的自动化类型。我的代码如下

I tried to implement passing C# string array to VB and VB string array to C# as below C#->VB working fine but other way (VB=>C#) giving a compile error called "Function or interface marked as restricted, or the function uses an automation type not supported in visual basic" . My code below

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

更新:
::::::

Update: ::::::

我们需要传递数组作为参考在C#。在界面和方法中更改它

We need to pass the array as reference in C#. Change it in the interface and method

void SetArray(ref string[] arrayVal ); //ref added


推荐答案

dotNETServer.SetArray (arr)

这实际上是迫使 arr 通过值传递,因为它被括号括起来,没有 Call 关键字。

This is actually forcing arr to be passed by value because it is enclosed by parentheses with no Call keyword.

您要这样做:

Call dotNETServer.SetArray(arr)

dotNETServer.SetArray arr

这篇关于将字符串数组从VB6传递到C#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆