2016-07-24 23 views
1

讓VB6說我有一個看起來是這樣的一個數組列表:通行證的ArrayList

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 ...

定義的屬性但是,當我引用我的目標是讓彈出錯誤:

Error description

即從VB6的對象數據傳遞到最簡單的方法.net對象? 除了多維數組之外的其他東西嗎?

+1

的ArrayList是一個保留字,所以它不是一個好主意來命名自己的類型,因爲這。傳遞數組應該可以正常工作 –

+0

即使當我更改類型的名稱時它也不起作用 –

回答

0

enter image description here

A「公共對象模塊中定義用戶定義類型」是指VB6類模塊相似的方式Vb.Net類定義VB.Net用戶類型。我沒有安裝VB6,但在其表弟語言VBA中,通過設置其「Instancing」屬性來公開課程。下面介紹的所有代碼都是使用VBA測試的,所以它也應該在VB6中工作。

聲明一個VB6類模塊,而不是像你一樣聲明一個UDT。

' 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