|
-
Apr 1st, 2007, 03:46 AM
#1
Thread Starter
Addicted Member
[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.
-
Apr 1st, 2007, 05:50 AM
#2
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.
-
Apr 1st, 2007, 05:53 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 1st, 2007, 05:55 AM
#4
Re: [2005] Is it possible for a function have multiple variables with in a return
Yes, fair point Rob, I stand corrected.
-
Apr 1st, 2007, 05:57 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 1st, 2007, 07:09 AM
#6
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.
-
Apr 1st, 2007, 07:37 AM
#7
Thread Starter
Addicted Member
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.
-
Apr 1st, 2007, 04:53 PM
#8
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|