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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.