Passing vb6 object type parameter byref from .NET interop causes Type Mismatch

Go To


I don't know much about Com Plus Interop services in .NET - I let .NET do all the dirty work and I cross my fingers it'll work. Well, now I'm stuck.

I've got a reference to a COM DLL compiled with VB6 in my VS 2010 C# program. This is an invoice I'm creating.

I instantiate an object:

UIInvoice Invoice = new CUIInvoice();

And then I set some invoice header properties:


And now, I want to create some invoice detail lines, which I do by calling the add method of a child object of the invoice:

Invoice.InvoiceDetails.Add("StringParam1", "StringParam2", Invoice);

The third parameter of the function call is defined in my VB6 Add function as:

ByRef Parent As Object

When I run my .NET program, I get a "type mismatch" error when I hit the Add line.

Can anyone suggest, in simplistic terms, a way I can get this to work?

2012-04-04 19:01
by user529703
You would normally write ref Invoice in C#. Guess you're using C# version 4. Nothing else is obvious, it just doesn't like CUIInvoice to be a parent. Maybe because of "UI" - Hans Passant 2012-04-04 20:29
Hi Hans, thanks for your reply. When I pass the Invoice object by ref, I actually have a compiler error - which may be a clue to those with more knowledge: cannot convert from 'ref UIInvoice.CUIInvoice' to 'ref object'. If I declare a static object type as in object temp = Invoice; and pass temp in by ref, I get the same type mismatch error - user529703 2012-04-05 11:10
What is the difference between UIInvoice and CUIInvoice? What happens if you declare Invoice as: CUIInvoice Invoice = new CUIInvoice(); - Tim Lentine 2012-04-05 13:23


My recommendation would be to not fool around with COM, but migrate the VB6 code to Visual Basic.NET. You'll have much less problems marshalling objects back and forth with C#. If this object is shared with other legacy applications, you may be able to create a COM wrapper that maintains your legacy compatibility as well. Obviously you need to weigh this against your business requirements.

2012-04-05 12:59
by mgnoonan
mgnoonan, Thanks for your comment. I believe you are right. I was hoping to short circuit the idea of migrating the code - it's too big. This may be a good reason to start the process - user529703 2012-04-05 15:55
That's usually how it starts, but in the end you wind up with more problems and bandage solutions than if you just dig in and do the migration. You'll be better off in the longer term - mgnoonan 2012-04-05 16:53


If you own the VB6 source then I suggest changing the definition of Add to be ByVal instead of ByRef. There is almost never a reason to use ByRef object references in VB6, and from the tiny bit I am gleaning from your code, you don't need to to associate paranet/child relationships in your domain objects.

2012-04-06 12:47
by tcarvin