Retrun Multipe variable values from a function

J

Jim

I there a way to return values for multiple variable from a fuction.
The following does not work because it give me a syntax error

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim a As Integer, b As Integer, c As Integer = 1
Dim d As Boolean = False
MsgBox(a & " " & b & " " & c)
d = Testit(a, b, c)
MsgBox(a & " " & b & " " & C)

End Sub
Public Function Testit(ByVal A As Integer, ByVal b As Integer,
ByVal c As Integer) As Boolean
A += 1
b += 1
c += 1
return a, b,c
End Function
 
M

Martin H.

Hello Jim,

a function can only return ONE variable.

If you want to return several values, you can choose between these options:

1. Use ByRef arguments

2. Return an array or collection

3. Return a string and separate values with a separator (BAD...)


1.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Boolean
A += 1
b += 1
c += 1
Return True ';-)
End Function

'Can also be a Sub...
Public Sub Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer)
A += 1
b += 1
c += 1
End Sub

2.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Integer()
Dim t(2) As Integer
t(0) = A + 1
t(1) = b + 1
t(2) = c + 1
Return t
End Function

Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Collection (Of Integer)
Dim c As Collection (Of Integer)
c.Add (A + 1)
c.Add (b + 1)
c.Add (c + 1)

Return c
End Function

3.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As String
Dim s As String
s = CStr(A + 1) & "|"
s &= CStr(b + 1) & "|"
s &= CStr(c + 1) & "|"

Return s
End Function

Best regards,

Martin
 
R

rowe_newsgroups

Hello Jim,

a function can only return ONE variable.

If you want to return several values, you can choose between these options:

1. Use ByRef arguments

2. Return an array or collection

3. Return a string and separate values with a separator (BAD...)

1.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Boolean
A += 1
b += 1
c += 1
Return True ';-)
End Function

'Can also be a Sub...
Public Sub Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer)
A += 1
b += 1
c += 1
End Sub

2.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Integer()
Dim t(2) As Integer
t(0) = A + 1
t(1) = b + 1
t(2) = c + 1
Return t
End Function

Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As Collection (Of Integer)
Dim c As Collection (Of Integer)
c.Add (A + 1)
c.Add (b + 1)
c.Add (c + 1)

Return c
End Function

3.
Public Function Testit(ByRef A As Integer, ByRef b As Integer,
ByRef c As Integer) As String
Dim s As String
s = CStr(A + 1) & "|"
s &= CStr(b + 1) & "|"
s &= CStr(c + 1) & "|"

Return s
End Function

Best regards,

Martin

You can also use a structure - which is my preferred way of doing
this.

' Typed in message

Public Structure MyStruct
Public Var1 As Integer
Public Var2 As Integer
End Structure

Public Function Foo() As MyStruct
Dim ms As New MyStruct
ms.Var1 = 12
ms.Var2 = 144
Return ms
End Function

Thanks,

Seth Rowe
 
Z

zacks

I there a way to return values for multiple variable from a fuction.
The following does not work because it give me a syntax error

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim a As Integer, b As Integer, c As Integer = 1
Dim d As Boolean = False
MsgBox(a & " " & b & " " & c)
d = Testit(a, b, c)
MsgBox(a & " " & b & " " & C)

End Sub
Public Function Testit(ByVal A As Integer, ByVal b As Integer,
ByVal c As Integer) As Boolean
A += 1
b += 1
c += 1
return a, b,c
End Function

I prefer to use a Class object to return multiple values.
 
G

Guest

Hello Jim,

a function can only return ONE variable.

If you want to return several values, you can choose between these
options:

1. Use ByRef arguments

2. Return an array or collection

3. Return a string and separate values with a separator (BAD...)

You can return an object or structure - in which case you can send any data
back :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top