locked
Passing array from .NET to VB6

    Question

  • 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() as Namespace.Product
    arr = manager.SearchProducts  'Doesnt work TYPE MISMATCH


    I've also read the article about the COM template in VB.NET in http://support.microsoft.com/kb/817248 Riquel 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
    Friday, October 10, 2008 7:28 AM

Answers

  • Read this thread. I can get the array object in VB6 application. See the following VB6 code.

    Private Sub Command1_Click() 
    Dim c As ClassLibrary7.ComClass1 
    Set c = New ClassLibrary7.ComClass1 
    Dim c1() As ClassLibrary7.ComClass2 
    c1 = c.myFunction 
    MsgBox c1(1).test 
    c1(1).test = 2143 
    MsgBox c1(1).test 
    End Sub 
     


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

All replies

  • 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 the actual namespace, just a placeholder) 

    Dim prod As Namespace.Product
    Set prod = Namespace.Product
     
    prod = manager.SearchProducts 'ERROR: Object doesnt support this property or 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 expression manager.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



    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
    Saturday, October 11, 2008 1:44 AM
  • Read this thread. I can get the array object in VB6 application. See the following VB6 code.

    Private Sub Command1_Click() 
    Dim c As ClassLibrary7.ComClass1 
    Set c = New ClassLibrary7.ComClass1 
    Dim c1() As ClassLibrary7.ComClass2 
    c1 = c.myFunction 
    MsgBox c1(1).test 
    c1(1).test = 2143 
    MsgBox c1(1).test 
    End Sub 
     


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