p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual Basic 2005 Basics (http://p2p.wrox.com/forumdisplay.php?f=137)
-   -   VB2005 trouble returning structure from function (http://p2p.wrox.com/showthread.php?t=50928)

MikeCS November 29th, 2006 06:25 PM

VB2005 trouble returning structure from function
Hi all

I would like some help with this issue. I am new to VB 2005 (OK with VB6)

My problem is that I cannot seem to return a structure from a function.

I defined a structure in a class (from which I instantiated an object).
One of the object methods is supposed to return a data record (a structure).

So, from the routine trying to acquire the record (a routine in a form class) I tried defining a local structure in order to receive the data from the object. But I got the following error:

Value of type 'CMA.class_ClassOne.structureOne' cannot be converted to 'CMA.frmFormOne.structureTwo'.
(even though they both have identical elements!)

Next, I tried defining a local variable of type object to recieve the data record. This seems to work in that I can seemingly retrieve the record, but now I cannot fill a datagridview with the data (which was the whole point). It seems as though it doesn't actually retrieve the data somehow. That is, I can assign the elements which (apparently) belong to the local object variable to particular rows and columns of the datagridview, but nothing happens: the grid remains blank.

More pictorially:

some class... some protected friend structure
(I had to make it friend, because I got an error that the function couldn't expose the structure if it was private)

... some function returns variable of type structure
(that is the above defined structure)

end some class

some other class (a form)
... define local structure to have the same elements as structure in some class (in order to retrieve data from some class)--- Get can't convert error

Tried this next:
... define local variable of type object: No error, but doesn't seem to work

end some other class

Any help to understand (and fix) what is going on here would be greatly appreciated. My project development has kind of stalled now.



ef1196 November 29th, 2006 07:45 PM


Here is one example using a module within your application:
Module mdlPublic

    '* Declare your structure PUBLIC to your application.
    Public Structure EmployeeInfo
        Dim FirstName As String
        Dim LastName As String
    End Structure

    '* This returns your structure as its return value.
    Public Function fncGetEmployeeInfo() As EmployeeInfo

        '* Create a local reference of your structure.
        Dim EmpInfo As New EmployeeInfo

        '* Here I'm just assigning values, but you
        '* could query a table for this info also
        '* as an example.
        With EmpInfo
            .FirstName = "John"
            .LastName = "Smith"
        End With

        '* Here you return the contents of the
        '* structure to your calling routine.
        fncGetEmployeeInfo = EmpInfo

    End Function

End Module


Best Regards,
Earl Francis

All times are GMT -4. The time now is 05:37 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.