4 Replies - 12611 Views - Last Post: 01 March 2012 - 08:34 PM Rate Topic: -----

#1 itsprs   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 12

Return more than one value from a function

Posted 01 March 2012 - 12:36 PM

Hi guys, this is my first question here with you guys. As I am new in this forum and in VB programming I will have more input in the future, but for now I greatly appreciate if you could help me to return two values from this function. One over time hours and one week's pay @10 hourly pay. I wanted to display those two in two listboxes 1 and 2. Thankks a lot!!

 Public Class Form1
    Dim overTimeHours As Double
    Dim weekPay As Double
    Dim hoursWork As Double
    Dim hourlyPay As Double

    Function DisplayVal(ByVal a As Double, ByVal b As Double)
        If a <= 40 Then
            overTimeHours = 0
            weekPay = b * a
        Else
            overTimeHours = a - 40
            weekPay = 40 * b + 1.5 * b * overTimeHours
        End If
        Return overTimeHours
        Return weekPay
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        hoursWork = TextBox1.Text
        hourlyPay = TextBox2.Text
        ListBox1.Items.Add(DisplayVal(hoursWork, hourlyPay))
        ListBox2.Items.Add(DisplayVal(hoursWork, hourlyPay))
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    End Sub

End Class

This post has been edited by smohd: 01 March 2012 - 12:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Return more than one value from a function

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: Return more than one value from a function

Posted 01 March 2012 - 12:50 PM

You could have to parameters passed as "by ref" and have those be filled by your function.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Return more than one value from a function

Posted 01 March 2012 - 01:50 PM

Of the my preferred why of creating a class / structure to encapsulate that concept.

Also try to rewrite the function so it only works on the input parameters provided, an not anything outside of it.

Function Pay(HoursWorked As Decimal, HourlyRate As Decimal) As Decimal
 ' calculation '
End Function

Was This Post Helpful? 0
  • +
  • -

#4 shadachi   User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Return more than one value from a function

Posted 01 March 2012 - 08:26 PM

I would suggest returning an object with properties containing both the values of One over time hours and one week's pay @10 hourly pay .

@Adam ,

Is it advisable to reference a heavily loaded object as a function's parameter and use the properties within that function or a better practice would be like you said just provide the input parameters , an not anything outside of it.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Return more than one value from a function

Posted 01 March 2012 - 08:34 PM

Why not encapsulate the entire object with it's own functions. So a custom class with exposed properties that you fill with functions that check internal variables and return results. For your example of returning 2 values you could simply pass the two labels to a Sub and in the sub you set them to values that were calculated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1