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

MattWilkinson
MattWilkinson used Ask the Experts™
on
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)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

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

Author

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.
Most Valuable Expert 2012
Top Expert 2008

Commented:
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...
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Yes if I reference it as you say I get the  'wrong number of arguments or invalid property  assignment' error
Top Expert 2010
Commented:
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

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial