-
May 5th, 2012, 08:14 PM
#1
Thread Starter
New Member
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
-
May 5th, 2012, 08:46 PM
#2
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.
-
May 9th, 2012, 12:32 PM
#3
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
-
May 9th, 2012, 12:44 PM
#4
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
-
May 9th, 2012, 02:10 PM
#5
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
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.
-
May 9th, 2012, 02:27 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|