Question:
Retrieving Multiple Return Values From Functions in VB.NET?
Andrew
2009-08-31 19:09:41 UTC
I was able to retrieve a single return value from a function, but I was having a hard time configuring how to retrieve multiple return values.

Ex:


// Form Code
dim z as integer

z = 0

function1(z)


// Function code

function function1(byval x as integer) as integer

dim a, b, c as integer

a = x + 1
b = x + 2
c = x + 3

return(a)
return(b)
return(c)

end function


***************************************

from the given example, I was able to retrieve the first return value (which is a) with this code in the form

label1.text = function1(z)

obviously, the result is 1

Now, how will I ever retrieve b and c?

Please help me!!!!
Three answers:
Kasey C
2009-08-31 19:20:21 UTC
You don't. Not with a function.



Use a Proc instead. And you won't be using RETURN either.



Function can ONLY return 1 value. That's why it's called a FUNCTION.



You see any mathematical functions that returns multiple values? Nope.



EDIT: the other answer basically made a "composite value" by using a user-defined type (in C++, it'd be a "struct") You'll still have to de-couple the values after you get it out. So it doesn't strictly return multiple values, it returns a "composite" value you need to take apart yourself.



---

Kasey C, PC guru since Apple II days

The secret of the universe is @*&^^^ NO CARRIER
Helpful Harry
2009-09-01 09:16:05 UTC
Sub Main



Dim a() As Int16 = {1, 2, 3}

Dim b As New List(Of Int16)

b.AddRange(my_fun(a))

For Each abc As Int16 In b

MsgBox(abc)

Next



End Sub





Function my_fun(ByVal str() As Int16)



Dim lst As New List(Of Int16)

lst.AddRange(str)

Return lst



End Function





Dane
Melanie W
2009-08-31 19:23:31 UTC
Here's an example.



Good luck!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...