假设在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对象的最简单方法是什么?除了多维数组之外,还有其他什么吗?
“在公共对象模块中定义的用户定义类型”是指VB6类模块,类似于Vb.Net类定义VB.Net用户类型的方式。我没有安装VB6,但是使用其表亲语言VBA,可以通过设置类的“ Instancing”属性来公开该类。下面介绍的所有代码均已使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句