【问题标题】:Pass arrayList from vb6 to vb.net object将 arrayList 从 vb6 传递给 vb.net 对象
【发布时间】:2016-07-24 18:53:17
【问题描述】:

假设在 vb6 中我有一个看起来像这样的数组列表:

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

Dim dataList() As ArrayList

dataList(0).str1 = "String 1"

这是我在 vb6 对象中填写的内容。现在我想将它传递给我的 vb.net 对象。

我在 vb.net 上定义了一个名为 Public Property WarrantyDetails As ArrayList ... 的属性

但是当我引用我的对象时,它会弹出错误:

将数据从 vb6 对象传递到 .net 对象的最简单方法是什么? 除了多维数组还有什么?

【问题讨论】:

  • ArrayList 是保留字,因此将自己的类型命名为 this 不是一个好主意。传递一个数组应该可以正常工作
  • 即使我更改类型的名称也不起作用

标签: arrays vb.net arraylist vb6


【解决方案1】:

“在公共对象模块中定义的用户定义类型”是指类似于 Vb.Net 类定义 VB.Net 用户类型的方式的 VB6 类模块。我没有安装 VB6,但是在它的表亲语言 VBA 中,您可以通过设置其“实例化”属性来公开该类。下面提供的所有代码都使用 VBA 进行了测试,因此它应该也可以在 VB6 中运行。

不要像你一样声明一个 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

在 VB.Net 端,您声明一个“COM 类”,其中包含一个接收 VB6 类实例的方法。请注意,这个 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

在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多