|
-
Sep 12th, 2014, 02:10 PM
#1
Thread Starter
New Member
have a function return multiple values
i want this function to return multiple values. I thought I was on track but i'm stuck. any help would be great. thank
Private Function get_offense(ByVal a As Double, ByVal b As Double, ByVal c As Double)
'Dim road As String
Dim road_conv As Double
'Dim home As String
Dim home_conv As Double
'Dim total As Double
Dim total_conv As Double
' get offense road value
Try
a = InputBox("Enter the offense road value", "Input Required", "0")
If a = "" Then
Exit Try
End If
road_conv = CDbl(a)
Catch ex As Exception
End Try
' get offense home value
Try
b = InputBox("Enter the offense home value", "Input Required", "0")
If b = "" Then
Exit Try
End If
home_conv = CDbl(b)
Catch ex As Exception
End Try
'get offense total value
Try
c = InputBox("Enter the offense total value", "Input Required", "0")
If c = "" Then
Exit Try
End If
total_conv = CDbl(c)
Catch ex As Exception
End Try
Return (road_conv)
Return (home_conv)
Return (total_conv)
End Function
-
Sep 12th, 2014, 02:36 PM
#2
Re: have a function return multiple values
A function can only return one item. That's it. Now... that's not to say that that one item can't return multiple items:
Code:
Public Class ConvValues
Public Property Road as Double
Public Propert Home as Double
Plublic Property Total as Double
End Class
Private Function get_offense(ByVal a As Double, ByVal b As Double, ByVal c As Double) as ConvValues
dim retValue as ConvValues = new ConvValues
' get offense road value
Try
a = InputBox("Enter the offense road value", "Input Required", "0")
If a = "" Then
Exit Try
End If
Double.TryParse(a, retValue.Road)
Catch ex As Exception
' If you're going to have a try-catch, you should at least catch the exception
End Try
' get offense home value
Try
b = InputBox("Enter the offense home value", "Input Required", "0")
If b = "" Then
Exit Try
End If
Double.TryParse(b, retValue.Home)
Catch ex As Exception
End Try
'get offense total value
Try
c = InputBox("Enter the offense total value", "Input Required", "0")
If c = "" Then
Exit Try
End If
Double.TryParse(c, retValue.Total)
Catch ex As Exception
End Try
Return retValue
End Function
Actually, I'd probably create a single form with all three values, have the user enter them all, then parse them out. Most people don't like using InputBox (myself included) as it's a pain. It also doesn't reflect real-world usage.
-tg
-
Sep 12th, 2014, 02:38 PM
#3
Re: have a function return multiple values
In short you can't. A function can have only a single return. You can however, create a structure that holds everything you need to reutrn, then return the structure.
VB Code:
Public Structure myStructure
Public road_conv As Double
Public home_conv As Double
Public total_conv As Double
End Structure
Private Function get_offense(ByVal a As Double, ByVal b As Double, ByVal c As Double) As myStructure
Dim returnStructure as New myStructure
' get offense road value
Try
a = InputBox("Enter the offense road value", "Input Required", "0")
If a = "" Then
Exit Try
End If
returnStructure.road_conv = CDbl(a)
Catch ex As Exception
End Try
' get offense home value
Try
b = InputBox("Enter the offense home value", "Input Required", "0")
If b = "" Then
Exit Try
End If
returnStructure.home_conv = CDbl(b)
Catch ex As Exception
End Try
'get offense total value
Try
c = InputBox("Enter the offense total value", "Input Required", "0")
If c = "" Then
Exit Try
End If
returnStructure.total_conv = CDbl(c)
Catch ex As Exception
End Try
Return (returnStructure )
End Function
Kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Sep 12th, 2014, 03:04 PM
#4
Thread Starter
New Member
Re: have a function return multiple values
Ok. I appreciate both of your help. i'll try and create a form that asks for the info and see if I can get it to work.
-
Sep 12th, 2014, 04:17 PM
#5
Re: have a function return multiple values
Although there is a method using Tuple class available which allows multiple values to be returned and each one can be a different type you are better off with the replies prior to mine as they provide useful names while using Tuple does not.
Example: (note how values are referenced ItemN where N is the ordinal position.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Example As Tuple(Of String, Integer, Date) = SomeFunction()
Console.WriteLine("String: {0} Number: {1} Today: {2}",
Example.Item1,
Example.Item2,
Example.Item3)
End Sub
Public Function SomeFunction() As Tuple(Of String, Integer, Date)
Dim SomeString As String = My.Computer.Info.OSVersion
Dim TheYear As Integer = Now.Year
Dim Today As Date = Now
Return New Tuple(Of String, Integer, Date)(SomeString, TheYear, Today)
End Function
-
Sep 13th, 2014, 02:48 AM
#6
Re: have a function return multiple values
i dont recomment it,but one could declare the function parameters ByRef
Code:
Function Doit(ByRef a As Double, ByRef b As Double, ByRef c As Double) As Double
a = a + 1
b = b + 1
c = c + 1
Return a + b + c
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aa, bb, cc As Double
aa = 1
bb = 1
cc = 1
Debug.Print(aa)
Debug.Print(bb)
Debug.Print(cc)
Debug.Print(Doit(aa, bb, cc))
Debug.Print(aa)
Debug.Print(bb)
Debug.Print(cc)
End Sub
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
|