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
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.
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
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.
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Const sqFt = 1000
Dim gal = Gallons(sqFt)
Dim cost = RoomPaintCost(gal, 19.95)
End Sub
Private Shared Function Gallons(squareFeet As Integer) As Decimal
Return Math.Ceiling(squareFeet / 115)
End Function
Private Shared Function RoomPaintCost(gallons As Decimal, paintCost As Decimal) As Decimal
Return gallons * paintCost
End Function
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.