How to expose an array to COM Interop and access it directly in VB6

I am creating a new class in VB.NET that will be used by .NET and VB6 applications. I want to create a public array of another class e.g.

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class clsTerminals
    Public Sub New()
        MyBase.New()
    End Sub
    Public Terminal() As clsTerminal
end class

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class clsTerminal
 Public ID as integer
end class

I changed the Class from <ComClass(clsTerminal.ClassId, clsTerminal.InterfaceId, clsTerminal.EventsId)>  to AutoDual because VB6 would not show the array at all before.

Now when I try the following in VB6 I get 'wrong number of arguments or invalid property assignment'

Dim vizTerminals As New VizualLib.clsTerminals
vizTerminals.LoadTerminals

MsgBox vizTerminals.Terminal(1).ID <- THIS LINE WILL NOT COMPILE



I have tried declaring my array as:
    Public Terminal(0) As clsTerminal

and also in the constructor redim Terminal(0)
MattWilkinsonAsked:
Who is Participating?
 
nffvrxqgrcfqvvcCommented:
You have to add your own indexing to return the specific object associated with that index. Instead of returning the clsTerminal() array return the object associated with that index.
In VB6 for example would look something like this with the update.
c.LoadTerminals
Debug.Print c.Terminal(4).Id

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class clsTerminals
    Private _Terminal() As clsTerminal
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub LoadTerminals()
        ReDim _Terminal(5)
        For i As Integer = 0 To 5
            _Terminal(i) = New clsTerminal
            _Terminal(i).ID = i
        Next
    End Sub
    Public Function Terminal(ByVal Index As Integer) As clsTerminal
        Return _Terminal(Index)
    End Function
End Class

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class clsTerminal
    Public ID As Integer
End Class

Open in new window

0
 
Bob LearnedCommented:
I don't work with arrays very much with COM, but I believe that the problem is that .NET arrays are zero-based.  Where are you defining the array?  BTW, I am usually more successful with COM interop when I declare the interface in .NET, and then have the .NET class implement that interface.
0
 
MattWilkinsonAuthor Commented:
The array is defined in the clsTerminals class (it is an array of the clsTerminal class), but I have tried creating an array of strings and this has similar problems.

VB6 is capable of using zero based arrays so it should in theory be capable of using them.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Bob LearnedCommented:
Yes, but you are dimensioning like this:

   Redim Terminal(0)

then, you would need to reference like this:

   MsgBox vizTerminals.Terminal(0).ID

unless I am misunderstanding the compiler error that you are getting...
0
 
MattWilkinsonAuthor Commented:
Yes if I reference it as you say I get the  'wrong number of arguments or invalid property  assignment' error
0
 
MattWilkinsonAuthor Commented:
Thanks - the key to getting this working was the following function

Public Function Terminal(ByVal Index As Integer) As clsTerminal
        Return _Terminal(Index)
    End Function

I didn't think that this would work and I was trying to do this in a property.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.