 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 19th, 2003, 11:34 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Methods - Returning Multiple Values?
Is it Possible to return multiple values?
I think I remember this being possible.
For example..
Sub returnValues(string1 as String, int1 as Integer, bol1 as Boolean)
Return string1, int1, bol1
End Sub
??
|
|

December 19th, 2003, 11:46 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
First thing to mention is that Subs don't return anything. You have to use "Function"
Functions can only return 1 thing. Normally, when you wanted to return multiple values you would do so in an array and return the array. You would then have to know the indexes of the pieces you wanted from the array.
The way I do this is to use a return argument class. I create a class with all the properties I expect to get back from the given operation.
For example, when I perform a database operation to retrieve some data, I create a class that contains properties like this:
- Boolean "Success"
- DataSet "ResultDataSet"
- String "Message"
- Exception "Exception"
My functions return an object of that class. I set the properties of the class to reflect what happened: did it succeed; here's the data; if it failed, here's the exception that was thrown by the internal code; etc.
This allows you to create functions with strongly typed return data, and makes it very easy to write the followup code for a function call because all the pieces of the return value are named and typed in a way that is clear.
Dim objResult As MyFunctionReturnArgs
objResult = MyFunction()
If objResult.Success Then
'Function succeeded, do something with the returned data
Else
'Function failed, display objResult.Message
' or handle the objResult.Exception
End If
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

December 21st, 2003, 08:19 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Or an alternative to using a class, is to create a structure. A structure is very similar to a class... a little more limited (in fact very limited), but ideal for simply creating value types.
ie:
Private Structure Peanut
'members
Public yourString as String
Public yourInteger as Integer
Public yourBoolean as Boolean
End Structure
Sub returnValues(yourPeanut as Peanut)
'your code here
Return yourPeanut
End Sub
|
|

December 21st, 2003, 08:26 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Opps! That should be:
Function returnValues()as yourPeanut
'your code here
Return yourPeanut
End Sub
or
Function returnValues(byVal etc etc)as yourPeanut
'your code here
Return yourPeanut
End Sub
!!!!!
|
|

December 21st, 2003, 11:53 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
In case the next question is "What's the difference between a Structure and a Class?"...
A structure is a complex data type like a class, however the class allows methods and property accessors. Typically a class would have private class fields providing the internal data storage and public property accessors that provide access to those internal values. In the scenario of return args, you would not likely have a class that had methods. One benefit to using a class is that you could make certain parts of the return args readonly. This way you could create the return arg object with a constructor that accepts values to be stored internally, but the caller of the function would get a return arg object with readonly properties. Just an added layer of data protection if necessary.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

December 22nd, 2003, 10:09 AM
|
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks!
I kinda forgot about structs as an alternative...the question arised because I seem to remember being able to return multiple values in vb6 from a sub and did not want to create classes or arrays.Oh and, dont worry about me in OOP, I know Java hehe, thanks.
|
|
 |