简体   繁体   English

将arrayList从vb6传递到vb.net对象

[英]Pass arrayList from vb6 to vb.net object

Let say in vb6 I have an array list that looks something like this: 假设在vb6中我有一个看起来像这样的数组列表:

 Public Type ArrayList
   str1 As String
   str2 As String
   str3 As String
 End Type

Dim dataList() As ArrayList

dataList(0).str1 = "String 1"

That is something I fill in a vb6 object. 那就是我填写的vb6对象。 Now I want to pass that to my vb.net object. 现在,我想将其传递给我的vb.net对象。

I defined a property at vb.net called Public Property WarrantyDetails As ArrayList ... 我在vb.net上定义了一个名为Public Property WarrantyDetails As ArrayList ...

But when I reference my object it gives popup error: 但是当我引用我的对象时,它会弹出错误:

错误说明

Which is the easiest way to pass data from vb6 object to .net object? 将数据从vb6对象传递到.net对象的最简单方法是什么? Anything other than multidimensional array? 除了多维数组之外,还有什么吗?

在此处输入图片说明

A "user-defined type defined in a public object module" means a VB6 Class Module similar to the way Vb.Net classes define VB.Net user types. “在公共对象模块中定义的用户定义类型”是指VB6类模块,类似于Vb.Net类定义VB.Net用户类型的方式。 I do not have VB6 installed, but in its cousin language VBA, you make the class public by setting its "Instancing" property. 我没有安装VB6,但是通过使用其表亲语言VBA,可以通过设置类的“ Instancing”属性来公开该类。 All the code presented below was tested using VBA, so it should also work in VB6. 下面介绍的所有代码均已使用VBA进行了测试,因此也应在VB6中运行。

Instead of declaring a UDT as you have, declare a VB6 Class Module. 而不是像声明那样声明UDT,而是声明VB6类模块。

' clsDemo
Option Explicit

Private str1_ As String
Private str2_ As String
Private str3_ As String

Public Property Get str1() As String
   str1 = str1_
End Property

Public Property Let str1(var As String)
   str1_ = var
End Property

Public Property Get str2() As String
   str2 = str2_
End Property

Public Property Let str2(var As String)
   str2_ = var
End Property

Public Property Get str3() As String
   str3 = str3_
End Property

Public Property Let str3(var As String)
   str3_ = var
End Property

On the VB.Net side, you declare a "COM Class" with a method to receive an instance of the VB6 class. 在VB.Net端,您可以使用一种方法来声明“ COM类”以接收VB6类的实例。 Note that this VB.Net class is declared with Option Strict Off to allow late-binding to the VB6 object members. 请注意,此VB.Net类是使用Option Strict Off声明的,以允许后期绑定到VB6对象成员。

Option Strict Off
Imports System.Runtime.InteropServices
Namespace Global
    <ComClass(Class1.ClassId, Class1.InterfaceId, Class1.EventsId)> _
    Public Class Class1
        Public Const ClassId As String = "0bf2556f-cc0f-420a-9ec5-a209fc967773"
        Public Const InterfaceId As String = "9c758eae-8eb0-4593-91cf-6a494fdcabb1"
        Public Const EventsId As String = "318f0ee0-8d5f-49b7-baa9-83cb8737cf57"

        Public Sub ReceiveVBAClass(obj As Object)
            MsgBox("str1 = " & obj.str1)
        End Sub

        Public Sub ReceiveVBAClassCollection(collection As Object)
            For Each o As Object In DirectCast(collection, System.Collections.IEnumerable)
                MsgBox("str1 = " & o.str1)
            Next
        End Sub
    End Class
End Namespace

On the VB6 calling side, the code would be similar to this: 在VB6调用端,代码类似于以下内容:

Sub TestToNet()
   Dim c1 As New TestReceiveVBAClassInstance.Class1
   Dim f As New clsDemo
   f.str1 = "hi"
   c1.ReceiveVBAClass f
End Sub

Sub TestToNet2()
   Dim coll As New Collection
   Dim f As clsDemo

   Set f = New clsDemo
   f.str1 = "hi"
   coll.Add f

   Set f = New clsDemo
   f.str1 = "there"
   coll.Add f

   Dim c1 As New TestReceiveVBAClassInstance.Class1
   c1.ReceiveVBAClassCollection coll
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM