使用Interop将对象数组从VB6返回到VB.NET

时间:2015-03-31 01:26:42

标签: .net vb.net dll vb6 interop

我在.NET中使用COM dll在VB6中使用,dll在VB6中运行良好。

但是当我想分配数组时,我遇到了问题。

这是VB.NET中的 Person 类, Name 属性是 Class2 的类数组强>

Public Class Person

  Private _name() As Class2

  Public Property Name() As Class2()
     Get
       Return Me._name
     End Get
     Set
       Me._name = value
     End Set
  End Property

End Class

这是 Class2

Public Class Class2

  Private m_id As String

  Public Property id() As String
    Get
      Return m_id
    End Get
    Set
      m_id = Value
    End Set
  End Property

End Class

这是我在VB6中使用的代码:

Dim vArray(2) As MyLib.Class2
vArray(0).id = "Hello 1"
vArray(1).id = "Hello 2"
vArray(2).id = "Hello 3"

Dim i As New MyLib.Person
i.Name = vArray ' here throws an error

1 个答案:

答案 0 :(得分:0)

尝试

 Dim vArray(2) As Class2
        For index = 0 To vArray.Length - 1
            vArray(index) = New Class2
            vArray(index).id = "Hello " + index.ToString
        Next
        Dim i As New Person
        i.Name = vArray