[2005] Is it possible for a function have multiple variables with in a return
I have a simple question about functions.
Once you create a function is it possible for that function's return statement to return the value of multiple variables. Example:
Function somefunction() as Decimal
If A > B Then
var1 = this number
var2 = that number
var3 = and so on...
End If
Return...(What would I put here to return all three variables?)
End Function.
I realize these questions seem elementary to a lot but I only have the minimal lecturing of my professor and a beginners book. I value this forum as a key source of information but I fear that a lot of the processes I am attempting to do with in our assignments either have not been covered or just can not be done.
Any help is appreciated.
Thanks:thumb:
Re: [2005] Is it possible for a function have multiple variables with in a return
You could return an array of values.
eg..
vb Code:
Function somefunction() as Decimal()
Dim array(2) as decimal
If A > B Then
array(0) = this number
array(1) = that number
array(2) = and so on...
End If
Return array
End Function.
Hope that helps a bit.
Re: [2005] Is it possible for a function have multiple variables with in a return
You shouldnt use "Array" for a variable as its a reserved word/type of object.
You can also return multiple variables if you pass them into the function ByRef.
Re: [2005] Is it possible for a function have multiple variables with in a return
Yes, fair point Rob, I stand corrected. :)
Re: [2005] Is it possible for a function have multiple variables with in a return
Just declare the return type as an Array or ArrayList. But if you only have a few args to return the ByRef may be a better choice.
It also sdepends on what and how you are going to use the return values.
Re: [2005] Is it possible for a function have multiple variables with in a return
Create a structure or class and have the function return that structure/class... In side the structure/class, you can declare as many different variables of different types as you want.
Re: [2005] Is it possible for a function have multiple variables with in a return
Hey Rob how would I incorporate that syntax at the end of the function if I wanted to return multiple variables if you pass them into the function ByRef. Don't mean to pose so many questions but I am rather new to functions and I don't get a lot of support from the staff at school. Just looking for an example to get me off the ground and running. I currently have 4 variables that need to be returned from each of the 8 functions I need to build for this application.
Thanks again guys
Re: [2005] Is it possible for a function have multiple variables with in a return
To return 4 variables from a function call:
vb Code:
Public Function SomeStuff(ByRef iVar1 As Integer, ByRef iVar2 As Integer, ByRef iVar3 As Integer, ByRef iVar4 As Integer) As Boolean
Try
'Do some stuff modifying the values of the vars
Return True
Catch ex As Exception
Return False
End Try
End Function
Then to call and pass the vars:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iVar1 As Integer
Dim iVar2 As Integer
Dim iVar3 As Integer
Dim iVar4 As Integer
If SomeStuff(iVar1, iVar2, iVar3, iVar4) = True Then
Messagebox.Show(iVar1.ToString & ", " & iVar2.ToString & ", " & iVar3.ToString & ", " & iVar4.ToString & ", ")
Else
Messagebox.Show("Error with 'SomeStuff'")
End If
End Sub