Results 1 to 8 of 8

Thread: [2005] Is it possible for a function have multiple variables with in a return

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    [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
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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:
    1. Function somefunction() as Decimal()
    2.  
    3.     Dim array(2) as decimal
    4.  
    5.     If A > B Then
    6.         array(0) = this number
    7.         array(1) = that number
    8.         array(2) = and so on...
    9.     End If
    10.  
    11.     Return array
    12.  
    13. End Function.

    Hope that helps a bit.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2005] Is it possible for a function have multiple variables with in a return

    Yes, fair point Rob, I stand corrected.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  7. #7

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Public Function SomeStuff(ByRef iVar1 As Integer, ByRef iVar2 As Integer, ByRef iVar3 As Integer, ByRef iVar4 As Integer) As Boolean
    2.     Try
    3.         'Do some stuff modifying the values of the vars
    4.         Return True
    5.     Catch ex As Exception
    6.         Return False
    7.     End Try
    8. End Function
    Then to call and pass the vars:
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim iVar1 As Integer
    3.     Dim iVar2 As Integer
    4.     Dim iVar3 As Integer
    5.     Dim iVar4 As Integer
    6.     If SomeStuff(iVar1, iVar2, iVar3, iVar4) = True Then
    7.         Messagebox.Show(iVar1.ToString & ", " & iVar2.ToString & ", " & iVar3.ToString & ", " & iVar4.ToString & ", ")
    8.     Else
    9.         Messagebox.Show("Error with 'SomeStuff'")
    10.     End If
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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