Anel Hodžić Anel Hodžić - 4 months ago 45
Vb.net Question

Pass arrayList from vb6 to vb.net object

Let say in vb6 I have an array list that looks something like this:

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. Now I want to pass that to my vb.net object.

I defined a property at vb.net called
Public Property WarrantyDetails As ArrayList ...


But when I reference my object it gives popup error:

Error description

Which is the easiest way to pass data from vb6 object to .net object?
Anything other than multidimensional array?

Answer

enter image description here

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. I do not have VB6 installed, but in its cousin language VBA, you make the class public by setting its "Instancing" property. All the code presented below was tested using VBA, so it should also work in VB6.

Instead of declaring a UDT as you have, declare a VB6 Class Module.

' 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. Note that this VB.Net class is declared with Option Strict Off to allow late-binding to the VB6 object members.

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:

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
Comments