Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Is it possible to return more than 1 value from another class for example I have a method:
VB
Public Shared Function GetCustomerData() as Boolean
Dim strMsg as String
Dim i as Integer

If txtNumber1.Text <> String.Empty AND txtNumber2.Text <> String.Empty Then
   i = txtNumber1.Text + txtNumber2.Text
Else
   strMsg="Requirement is empty"
End If

Return i
End Function



This is an example of my code which I call the function method located in other class and want to return a 2 value that is "i" and "strMsg"

Could someone help me solve this? I've tried "Return i, strMsg" but it got blue underline string
Posted
Updated 8-Apr-12 17:03pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Apr-12 23:04pm    
From a class or a function? Your sample shows a function.
--SA
Luiey Ichigo 8-Apr-12 23:51pm    
it's a function a located in other project of the main system

Create a structure to hold the return data, then populate an instance of the structure and return that.
 
Share this answer
 
Comments
VJ Reddy 9-Apr-12 0:05am    
To the point. +5
There is no such concept as "return a value from class", simply it can mean just about anything. You can return a value only from a function.

There are two ways of doing that.

Fist one is using a data type encapsulating two or more values. It could be a class or a structure.

The second way is using out parameter. This feature is not implemented in VB.NET, but you can use passing parameters by reference, like in this example:
VB
Private Function GetCustomerData(ByRef index As Integer, ByRef message As String) As Boolean
    index = 13
    message = "message"
    Return True
End Function


In this sample, you return three different parameters of three different type. The Boolean parameters is returned on stack, and the other two are "populated in-place" by the references passed in the function.

—SA
 
Share this answer
 
v2
Comments
Luiey Ichigo 8-Apr-12 23:54pm    
Oh! That explain to me..now I totally understand..send a parameter and put the new value then return true. so if i want to grab the value after it, i just call the parameter that send the value to this function.am i right? tq SA
Sergey Alexandrovich Kryukov 9-Apr-12 0:11am    
You do not "call the parameter" (what is that?). First of all, you can return any of the types as a return value (on stack or its reference on stack), not just Boolean. This is how you use ByRef: say, you create a local variable indexValue and assign its some value, say, 10. Then you call GetCustomerData and use the variable to substitute the formal parameter. The ByRef parameter will pass the reference of the actual parameter instead its value. The value can be used inside function by this references, but you can also assign a different value to is (13 is shown in a code sample). This value will be preserved after return to a caller, because the function body worked with the same reference to the value, not with the copy of the value. If you do same thing with the by-value parameter, you use the copy of the value, so the value of the variable representing the actual parameter won't be modified on return.

For a record: returning a single return value using the structure or class type is better in most cases.
--SA
VJ Reddy 9-Apr-12 0:05am    
To the point. +5
Sergey Alexandrovich Kryukov 9-Apr-12 0:12am    
Thank you, VJ.
--SA
Luiey Ichigo 9-Apr-12 0:17am    
Thank you sir..
What is given in Solution 2 by SAKryukov and in Solution 1 by Dave Kreskowiak is absolutely correct answer.
I want to add that in .NET FrameWork 4 and above a Tuple class is provided to readily create a data structure with multiple members as explained here
About Tuple Class on MSDN[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Apr-12 0:13am    
Yes, I wanted to mention Tuple but did not choose to spend extra time on that. Thank you for adding this information. My 5.
--SA
VJ Reddy 9-Apr-12 1:56am    
Thank you, SA.
member60 18-Apr-12 6:47am    
my 5!
VJ Reddy 18-Apr-12 7:05am    
Thank you, member60

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900