Results 1 to 7 of 7

Thread: how to get multiple return values of function in vb.net?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    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?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    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?

  3. #3
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: how to get multiple return values of function in vb.net?

    Quote Originally Posted by Bulldog View Post
    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    258

    Re: how to get multiple return values of function in vb.net?

    Quote Originally Posted by dday9 View Post
    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
  •  



Click Here to Expand Forum to Full Width