-
Mar 20th, 2015, 10:25 AM
#1
Thread Starter
Hyperactive Member
how to get multiple return values of function in vb.net?
For one function in vb.net, I want to get multiple return values of this function like a1, a2, a3(a1, a2 a3 are parameters calculated in this function), so when run this function how to get these values?
-
Mar 20th, 2015, 10:44 AM
#2
Thread Starter
Hyperactive Member
Re: how to get multiple return values of function in vb.net?
Seems like in VBA "call func(a1,a2,a3)" in vb.net call func(a1,a2,a3) cannot works, so how to get the value of a1 a2 a3?
-
Mar 20th, 2015, 11:10 AM
#3
Re: how to get multiple return values of function in vb.net?
You can do this using ByRef
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = 0
Dim y As Integer = 0
Dim z As Integer = 0
Test(x, y, z)
MessageBox.Show(y.ToString() + " " + z.ToString())
End Sub
Private Sub Test(ByVal a, ByRef b, ByRef c)
b = a + 1
c = a + 2
End Sub
There is also the "Tuple" class.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Mar 20th, 2015, 11:18 AM
#4
Thread Starter
Hyperactive Member
Re: how to get multiple return values of function in vb.net?
Originally Posted by Bulldog
You can do this using ByRef
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = 0
Dim y As Integer = 0
Dim z As Integer = 0
Test(x, y, z)
MessageBox.Show(y.ToString() + " " + z.ToString())
End Sub
Private Sub Test(ByVal a, ByRef b, ByRef c)
b = a + 1
c = a + 2
End Sub
There is also the "Tuple" class.
I need several notification like "call func(a1,a2,a3) and then use the values like E=a1, F=a2,G=a3. Does that work?
call func(a1,a2,a3)
E=a1
F=a2
G=a3
-
Mar 20th, 2015, 11:20 AM
#5
Re: how to get multiple return values of function in vb.net?
To elaborate what bulldog suggested, the ByRef parameter changes the argument being passed within the method where as the ByVal parameter does just the opposite. Take the following example:
Code:
Sub Main()
Dim foo As Boolean = True
Call ValFoo(foo)
Console.WriteLine(foo.ToString) 'foo = True
Call RefFoo(foo)
Console.WriteLine(foo.ToString) 'foo = False
End Sub
Private Sub ValFoo(ByVal x As Boolean)
x = False
End Sub
Private Sub RefFoo(ByRef x As Boolean)
x = False
End Sub
I once needed this for a design that I was using for a parser where I needed to return a Boolean based on if the source code parsed, but at the same time I needed to return an error if one was thrown. So I structured my method like such:
Code:
Private Function Parse(ByVal source As String, ByRef ex As Exception) As Boolean
-
Mar 20th, 2015, 11:20 AM
#6
Re: how to get multiple return values of function in vb.net?
There is also the Tuple class which is perhaps more powerful;
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = 0
Dim y As Integer = 0
Dim z As Integer = 0
With Test(x)
y = .Item1
z = .Item2
End With
End Sub
Public Function Test(ByVal x As Integer) As Tuple(Of Integer, Integer)
Return New Tuple(Of Integer, Integer)(x + 1, x + 2)
End Function
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Mar 20th, 2015, 12:41 PM
#7
Thread Starter
Hyperactive Member
Re: how to get multiple return values of function in vb.net?
Originally Posted by dday9
To elaborate what bulldog suggested, the ByRef parameter changes the argument being passed within the method where as the ByVal parameter does just the opposite. Take the following example:
Code:
Sub Main()
Dim foo As Boolean = True
Call ValFoo(foo)
Console.WriteLine(foo.ToString) 'foo = True
Call RefFoo(foo)
Console.WriteLine(foo.ToString) 'foo = False
End Sub
Private Sub ValFoo(ByVal x As Boolean)
x = False
End Sub
Private Sub RefFoo(ByRef x As Boolean)
x = False
End Sub
I once needed this for a design that I was using for a parser where I needed to return a Boolean based on if the source code parsed, but at the same time I needed to return an error if one was thrown. So I structured my method like such:
Code:
Private Function Parse(ByVal source As String, ByRef ex As Exception) As Boolean
Thanks it works, thanks for your patient
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|