在VB6访问.NET集合NET

2023-09-04 06:21:35 作者:感情很骨感

我有一个.NET程序集,有需要从VB6的DLL调用的例行程序。 .NET程序集的程序,对于其他.NET code将返回对象的名单。但这不会对VB6工作。所以我使用互操作来创建一个VB6类,将返回所需的数据。我曾读到,VB.NET收集与VB6集兼容,但我发现,是不真实的。我简单的测试包括:

I have a .NET assembly that has routines that need to be called from a VB6 dll. The .NET assembly's routines, for other .NET code will return Lists of objects. However that won't work for VB6. So I am using Interop to create a "vb6 class" that will return the data needed. I had read that the VB.NET Collection is compatible with the VB6 Collection, but I've found that to be untrue. My simple test consists of:

.NET code:

.NET Code:

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class MyCOMClass

    Public Function TestMe() As Microsoft.VisualBasic.Collection
        Dim ret As New Microsoft.VisualBasic.Collection

        Dim inParam As String = "Stuff "
        ret.Add(inParam)
        ret.Add(inParam & "2")
        ret.Add(inParam & "3")
        ret.Add(inParam & "4")

        Return ret
    End Function


End Class

VB6:

Dim a As MyDotNet.MyCOMClass

Set a = New MyDotNet.MyCOMClass

Dim c As Collection

Set c = a.TestMe()

在这一行,我收到一个类型不匹配,错误13错误。

At this line, I'm receiving a "Type Mismatch, Error 13" error.

我有点不知所措。基本上,我需要从.NET code返回的项目列表或数组 - 我已经将不得不收拾现有的.NET类对象转换成字符串或东西返回VB6(将要解压缩),所以我想使它稍微容易自己。

I'm kind of at a loss. I basically need to return a list or array of items from the .NET code - I'm already going to have to pack the existing .NET class object into a string or something to return to VB6 (which will then have to unpack it), so I was trying to make it slightly easier on myself.

任何建议或提示将AP preciated!

Any suggestions or hints will be appreciated!

感谢。

推荐答案

Microsoft.VisualBasic.Collection 兼容成员明智的,但它不是同一类型。

Microsoft.VisualBasic.Collection is compatible member-wise, but it's not the same type.

为什么不直接返回数组? ?字符串,或者你的COM可见.NET类 或创建一个索引属性?

Why not just return an array? Of strings, or of your COM-visible .NET classes? Or create an indexed property?

有了这样说,为什么不回到的IList 摆在首位? 的IList 是COM可见。 这工作:

Having that said, why not return IList in the first place? IList is COM-visible. This works:

<Microsoft.VisualBasic.ComClass()> _
Public Class Class1

    Public Function Test() As IList
        Dim l() As String = New String() {"abc", "def", "42"}
        Return l
    End Function

End Class

&NBSP;

Private Sub Command1_Click()
  Dim c As New ClassLibrary1.Class1

  MsgBox c.Test(2)
End Sub