Visual Basic Development Bookmark and Share   
 Home > Visual Basic Interop and Upgrade > Passing array from .NET to VB6
 

Passing array from .NET to VB6

Hi everyone.

I'm trying to call a method of .NET assembly from VB6. The method returns an array of a custom class I created. I have used this class in the VB6 application before since there are methods in the .NET assembly that receive parameters of that type (single parameters). However, now i have a method that returns an array of these objects and when I try to assign the result of the method to a varible in vb6 I get the error TYPE MISMATCH.


' Product is one of the classes exposed to COM
Dim product as Namespace.Product
product.Name = "Test"
product.Price = 100

' manager is an object of another COM class in the same .NET assembly
manager.CreateProduct product 'This works fine

'However the method that return Product[] iin .NET does not work in VB6
For Each a In manager.SearchProducts 'Doesnt work TYPE MISMATCH

'Code inside ...
Next

or even If I try
Dim arr() asNamespace.Product
arr =manager.SearchProducts'Doesnt work TYPE MISMATCH


I've also read the article about the COM template in VB.NET inhttp://support.microsoft.com/kb/817248Riquel suggested and make sure that everything is fine in the code, but I still have the problem. Besides, I have used the class before in other methods so I think it is not a problem exposing the types to COM, it must be something related to arrays handling.

Any help would be appreciated a lot,

Thanks in advance
Hugo.Vallejo  Friday, October 10, 2008 7:28 AM
Read this thread. I can get the array object in VB6 application. See the following VB6 code.

PrivateSubCommand1_Click()
DimcAsClassLibrary7.ComClass1
Setc=NewClassLibrary7.ComClass1
Dimc1()AsClassLibrary7.ComClass2
c1=c.myFunction
MsgBoxc1(1).test
c1(1).test=2143
MsgBoxc1(1).test
EndSub


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Tuesday, October 14, 2008 12:20 PM
Hi everyone.

I made two tests to try to understand what is going on.

First, I added a watch in VB6 to inspect the result of the method call. The result was:

Expression: manager.SearchProducts
Value: <Type mismatch>
Type: Integer

Then I changed the method. Instead of returning an array of Product objetcs the method only returns one object, but now I get a different error :OBJECT DOESNT SUPPORT THIS PROPERTY OR METHOD.
I call the method from vb6 like this:
(Namespace is not theactual namespace, just a placeholder)

Dim prod As Namespace.Product
Set prod =Namespace.Product

prod = manager.SearchProducts 'ERROR: Object doesnt support this propertyor method

I attached the visual studio debugger to VB6.exe and I can see that the code reaches the final return statement in the .NET assembly so it must be something when the control returns to vb6, vb6 is not able to marshal the object.

I also tried using a variant variable but I get the same error.

However, and this really confuses me, If I call the method without assigning the result to a variable the program runs with any errors:

Dim prod As Namespace.Product
Set prod =Namespace.Product

manager.SearchProducts
'No error, code execution continues

Moreover, if I add a watch on the expressionmanager.SearchProducts I can see the results on the watch window in VB6.
The results's type is Object/Product (remeber that the method returns only 1 object of type Product). All the properties are right, all the values are correct, I just can't assign the result to a variable in VB6!!!!!


And please take into account that I have used the Product class before as an input parameter of a method in the .NET assmebly. So, the problems appear when the data is passed from .NET to VB6 not the opposite. Also, the Product class contains only simple data, strings and one double field, no complex data, no arrays.

Any help about how to resolve this issue would be appreciated a lot,

Thanks in advance



Hugo.Vallejo  Friday, October 10, 2008 11:36 PM
Hi everyone.

I just noticed that in order to assign the result of the SearchProducts method to a variable in VB6 I must use the SET statement. Using the Set stament the assigement worked (this is for only 1 object returned from .NET to VB6).

However I still have the problem when returning an array of objects. Not event in the watch window I am able to see the array.

Hugo
Hugo.Vallejo  Saturday, October 11, 2008 1:44 AM
Read this thread. I can get the array object in VB6 application. See the following VB6 code.

PrivateSubCommand1_Click()
DimcAsClassLibrary7.ComClass1
Setc=NewClassLibrary7.ComClass1
Dimc1()AsClassLibrary7.ComClass2
c1=c.myFunction
MsgBoxc1(1).test
c1(1).test=2143
MsgBoxc1(1).test
EndSub


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Riquel_Dong  Tuesday, October 14, 2008 12:20 PM

You can use google to search for other answers

Custom Search

More Threads

• Installing Microsoft Interop Forms Toolkit 2.0 for Visual Studio 2008
• Converting VB6 to VS2008 - Trouble reading a field value.
• converted vb 6.0 data environment
• Word AddToRecentFiles issues with .Net
• Why Ctrl+break doesn't work in Vb2008?
• Interop Forms Toolkit: support for ownerform argument for Show()????
• Error 8007000e using Interop dll from VB.NET
• Upgrading to VB.NEt
• Refence Book reccomendations?
• Font charset problem