Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 29th, 2006, 06:25 PM
Registered User
 
Join Date: Nov 2006
Location: Edmonton, AB, Canada.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Example:
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.

Sincerely,

--Mike
Reply With Quote
  #2 (permalink)  
Old November 29th, 2006, 07:45 PM
Authorized User
 
Join Date: Nov 2006
Location: , , USA.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Mike,

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function returning an array rfstuck Pro VB 6 5 March 12th, 2008 08:35 AM
Function not returning value civa Access 5 January 17th, 2006 03:44 AM
Trouble with returning parameter values EricJ VB.NET 0 July 5th, 2005 09:58 AM
Function not returning anything... goatboy Beginning PHP 2 December 1st, 2003 12:34 PM
Returning variables from a function starsol Beginning PHP 1 September 11th, 2003 01:58 PM



All times are GMT -4. The time now is 11:15 AM.


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