Results 1 to 6 of 6

Thread: a single function with different returns

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    Question a single function with different returns

    hello,

    How can I call individual parts of this function? I would like to call return Gallons and decPaintCostperRoom as individual entities with a single function.

    Thank you for your time

    Code:
     Private Function Gallons(ByRef intSquareFeet As Integer, ByRef decPaintCost As Decimal) As Decimal
    
            Dim decGallons As Decimal
            Dim decPaintCostPerRoom As Decimal
    
            'gallons is averaged by 115
            decGallons = intSquareFeet / 115
            decGallons = Math.Ceiling(decGallons)
    
    
            decPaintCostPerRoom = decPaintCost * decGallons
    
    
            Return decGallons
            Return decPaintCostPerRoom

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: a single function with different returns

    You can only return one value. A value however can be a reference to anything you want, such as an object that encapsulates many other values. An existing framework 4+ object that might be useful to you is a Tuple. Alternatively you could define your own type using a class or structure or you could pass arguments ByRef (as you are) and alter them in the function.
    W o t . S i g

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: a single function with different returns

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: a single function with different returns

    I think you will want to return one of those values as the function return, and return the other in a ByRef argument.
    My usual boring signature: Nothing

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: a single function with different returns

    Rather than creating your own class to return or having a ByRef parameter for the function I would advise you to split it into 2 functions. Each method should have a single responsibility.

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         Const sqFt = 1000
    3.         Dim gal = Gallons(sqFt)
    4.         Dim cost = RoomPaintCost(gal, 19.95)
    5.     End Sub
    6.  
    7.     Private Shared Function Gallons(squareFeet As Integer) As Decimal
    8.         Return Math.Ceiling(squareFeet / 115)
    9.     End Function
    10.  
    11.     Private Shared Function RoomPaintCost(gallons As Decimal, paintCost As Decimal) As Decimal
    12.         Return gallons * paintCost
    13.     End Function
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: a single function with different returns

    I'm changing my answer. After looking at the code a bit closer, I totally agree with MattP: These are two functions.
    My usual boring signature: Nothing

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