Results 1 to 6 of 6

Thread: have a function return multiple values

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    4

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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:
    1. Public Structure myStructure
    2.       Public road_conv As Double
    3.       Public home_conv As Double
    4.       Public  total_conv As Double
    5. End Structure
    6.  
    7. Private Function get_offense(ByVal a As Double, ByVal b As Double, ByVal c As Double) As myStructure
    8.  
    9. Dim returnStructure as New myStructure
    10.  
    11. ' get offense road value
    12. Try
    13. a = InputBox("Enter the offense road value", "Input Required", "0")
    14. If a = "" Then
    15. Exit Try
    16. End If
    17. returnStructure.road_conv = CDbl(a)
    18. Catch ex As Exception
    19.  
    20. End Try
    21.  
    22. ' get offense home value
    23. Try
    24. b = InputBox("Enter the offense home value", "Input Required", "0")
    25. If b = "" Then
    26. Exit Try
    27. End If
    28. returnStructure.home_conv = CDbl(b)
    29. Catch ex As Exception
    30.  
    31. End Try
    32.  
    33. 'get offense total value
    34. Try
    35. c = InputBox("Enter the offense total value", "Input Required", "0")
    36. If c = "" Then
    37. Exit Try
    38. End If
    39. returnStructure.total_conv = CDbl(c)
    40. Catch ex As Exception
    41.  
    42. End Try
    43.  
    44.  
    45. Return (returnStructure )
    46.  
    47. 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

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    4

    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.

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

  6. #6
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    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
  •  



Click Here to Expand Forum to Full Width