9 Replies - 2444 Views - Last Post: 13 September 2012 - 05:50 PM Rate Topic: -----

#1 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Computing Weekly Gross Pay

Posted 13 September 2012 - 10:58 AM

Public Class Form1

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click

        HourlyRateTextBox.Clear()
        HoursWorkedTextBox.Clear()
        GrossPayTextBox.Clear()

    End Sub

    Private Sub CalculateGrossPay(ByVal hourlyrate As Decimal, _
                                 ByVal hoursworked As Decimal, _
                                 ByVal salaried As Decimal, _
                                 ByRef grosspay As Decimal)

        For index As Integer = 1 To grosspay

        Next
        grosspay = hourlyrate * hoursworked

        If hoursworked > 40 Then
            grosspay = hoursworked * hourlyrate _
                + (hoursworked - 40) * hourlyrate * 0.5
        End If

        
    End Sub

    'Private Function GrossPay(ByVal hourlyrate As Decimal,
    '                             ByVal hoursworked As Decimal,
    '                            ByVal salaried As Decimal) _
    '                           As Decimal

    '    For i As Integer = 1 To salaried
    '       GrossPay = hoursworked * hourlyrate
    '  Next

    '    If hoursworked > 40 Then
    '       GrossPay = hoursworked * hourlyrate _
    '          + (hoursworked - 40) * hourlyrate * 0.5
    ' End If

    '    Return GrossPay

    'End Function

    Private Sub ProcedureButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcedureButton.Click
        Dim hourlyrate As Decimal = CDec(HourlyRateTextBox.Text)
        Dim hoursworked As Decimal = CDec(HoursWorkedTextBox.Text)
        Dim grosspay As Decimal


        Dim salaried As Decimal
        Me.CalculateGrossPay(hourlyrate, hoursworked, salaried, grosspay)

        GrossPayTextBox.Text = FormatCurrency(grosspay)
        GrossPayTextBox.Select()

        

    End Sub

    Private Sub FunctionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FunctionButton.Click
       
        Dim hourlyrate As Decimal
        Dim grosspay As Decimal

        If SalariedCheckBox.Checked Then
            grosspay = hourlyrate * 40
        End If
    End Sub

   
    Private Function Salary() As Boolean

        Dim grosspay As Decimal
        Dim hourlyrate As Decimal

        If SalariedCheckBox.Checked Then
            grosspay = hourlyrate * 40
        End If
        Salary = True
    End Function
   

End Class



I am creating a program that creates a function and a sub procedure that computes gross pay. Now I am trying to get a salaried checkbox to be a boolean value so it can idicate whether the employee salaried (true) or not (false). This is what I have so far but it is not working. Then I have to put it in the function button and procedure button so I can calculate it. If the employee is salaried - the gross pay is simply 40 times the hourly rate no matter how many hours are working in the week. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Computing Weekly Gross Pay

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 11:04 AM

Be more specific. What isn't working. Have you tried setting breakpoints and stepping through your code? What error messages if any do you get?
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 11:39 AM

This might be an order of operation issue

grosspay = (hoursworked * hourlyrate) + ((hoursworked - 40) * (hourlyrate * 0.5))

With this the total work is calculated say 45hrs @ 10.00/hr

this will perform: 450 + (5 * 5) or 450 + 25 = 475


Sorry, I guess I should have read your post instead of assuming a calculation error.

Anyway. This should probably be a function that returns a decimal. Then you would send it Hours, rate and salaried

Private Function CalculateGrossPay(ByVal HourlyRate as Decimal, HoursWorked as Decimal, Salaried as Boolean) As Decimal


inside this function you would check if salaried is true

If it's true; Return HourlyRate * HoursWorked
If it's false; Return your grosspay calculation for overtime

Now the key is when calling the function. It's really nothing more than

Dim GrossPay as Decimal = GetGrossPay(10.25, 45, chkSalaried.Checked)


where chkSalaried is a checkbox on your form. If it's checked then it will return the salaried calculation, false will return the overtime calculation.

This post has been edited by CharlieMay: 13 September 2012 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 11:43 AM

Public Class Form1

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click

        HourlyRateTextBox.Clear()
        HoursWorkedTextBox.Clear()
        GrossPayTextBox.Clear()
        SalariedCheckBox.Checked = False

    End Sub

    Private Sub CalculateGrossPay(ByVal hourlyrate As Decimal, _
                                 ByVal hoursworked As Decimal, _
                                 ByVal salaried As Decimal, _
                                 ByRef grosspay As Decimal)

        For index As Integer = 1 To grosspay

        Next
        grosspay = hourlyrate * hoursworked

        If hoursworked > 40 Then
            grosspay = hoursworked * hourlyrate _
                + (hoursworked - 40) * hourlyrate * 0.5
        End If

        
    End Sub

    'Private Function GrossPay(ByVal hourlyrate As Decimal,
    '                             ByVal hoursworked As Decimal,
    '                            ByVal salaried As Decimal) _
    '                           As Decimal

    '    For i As Integer = 1 To salaried
    '       GrossPay = hoursworked * hourlyrate
    '  Next

    '    If hoursworked > 40 Then
    '       GrossPay = hoursworked * hourlyrate _
    '          + (hoursworked - 40) * hourlyrate * 0.5
    ' End If

    '    Return GrossPay

    'End Function

    Private Sub ProcedureButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcedureButton.Click
        Dim hourlyrate As Decimal = CDec(HourlyRateTextBox.Text)
        Dim hoursworked As Decimal = CDec(HoursWorkedTextBox.Text)
        Dim grosspay As Decimal
        Dim salaried As Decimal

        Me.CalculateGrossPay(hourlyrate, hoursworked, salaried, grosspay)

        GrossPayTextBox.Text = FormatCurrency(grosspay)
        GrossPayTextBox.Select()

        If Salary() Then
            hourlyrate = CInt(HourlyRateTextBox.Text)
            grosspay = hourlyrate * 40

            CalculateGrossPay(hourlyrate, hoursworked, salaried, grosspay)
            GrossPayTextBox.Text = FormatCurrency(grosspay)
        End If



    End Sub

    Private Sub FunctionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FunctionButton.Click
       
        Dim hourlyrate As Integer
        Dim grosspay As Decimal

        If Salary() Then
            hourlyrate = CInt(HourlyRateTextBox.Text)
            grosspay = hourlyrate * 40
            GrossPayTextBox.Text = FormatCurrency(grosspay)
        End If


    End Sub

   
    Private Function Salary() As Boolean

        Dim hourlyrate As Integer
        Dim grosspay As Decimal

        If SalariedCheckBox.Checked Then
            grosspay = hourlyrate * 40
        End If
        Salary = True
    End Function
   

End Class




This is what I added from the previous code. When I check the salary box I get a different answer for the procedure and the function and the exact same thing when I dont check the salary box. The function and procedure should be $475 should be the gross pay when you dont check the salary box and $400 when you do check the salary box. What I am having trouble is figuring out the not salaried function button and the procedure salaried button. When you check the salary box the function button and the procedure button should be the same and the when you dont check the salary box the function and procedure button should also be the same number.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 12:29 PM

One of the reasons you're having such a hard time is you've got logic all over the place. I would suggest you scrap what you currently have and do it over again.

As I understand it you need to do the following:

1. calculate the gross pay of a hourly employee
2. calculate the gross pay of a Salaried employee
3. Display the gross pay
4. You must have both a Function and a procedure

And you determine of the user is hourly or Salary by a checkbox on the form.

I'm not sure what you mean by procedure but there are two types of methods Functions which return a value and Subs that do not return a value. I would assume that you're teacher is saying you need to use one of each.

I'm going to assume you have already setup the form with the textobx box for hourly rate, Hours worked, and Gross pay. I also will assume you have the checkbox for determining if they are salary/hourly, and two buttons. One that will use a function and one that will use the sub.

Now to setup your code:

Create a function that takes in 2 decimals and a boolean (Hourly rate, Hours Worked, and if salaried/hourly) and returns a decimal value back.


public Function Foo(ByVal a as Decimal, ByVal b as Decimal, ByVal c as Boolean) As Decimal

End Function 



now, you already know how to do the calculations, so as far as the hourly/salary thing just use an if statement to determine which calculation to use

If c then 

else 

end if




now within the function you can eiter create a variable of type decimal to hold the grosspay value and return it at the end of the function, or just put the calculations in the return statement like so:
return a * b 



Now in the click event of your function button you would set the grosspay textbox text property equal to the return of the function, and you would also pass in the values of the hourly rate textbox, hours worked textbox and the Checked value of the checkbox:

Dim a as Decimal = Decimal.Parse(Hourly.Text)
Dim b as Decimal = Decimal.Parse(Hours.Text)
TextBox.Text = Foo(a,b,Salaried.Checked).ToString()



you would do this exact same thing with a sub except instead of returning a value you have two options. add a fourth Decimal parameter to your sub call that is passed ByRef that holds the gross pay, or use a global variable to hold gross pay and set it's value from within the sub. I don't tend to recommend the pass by ref as it can cause issues down the line. You could also just set the gross pay textbox's text property from within the sub, but then you're making your method have more then one function which should be avoided aswell.

This post has been edited by rgfirefly24: 13 September 2012 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 12:40 PM

whats the loop for?
You calculate gross pay, then you check if the hoursWorked > 40.
If it is, you calculate gross pay again?

View PostJakeDay, on 13 September 2012 - 02:43 PM, said:

       For index As Integer = 1 To grosspay

        Next
        grosspay = hourlyrate * hoursworked

        If hoursworked > 40 Then
            grosspay = hoursworked * hourlyrate _
                + (hoursworked - 40) * hourlyrate * 0.5
        End If       
    End Sub


The other thing I'd suggest is actually parsing your textbox data with the tryparse method.
So for lines like:
        Dim hourlyrate As Decimal = CDec(HourlyRateTextBox.Text)
        Dim hoursworked As Decimal = CDec(HoursWorkedTextBox.Text)


you'd do something like
it takes the string data from the text box and tries to convert it to, in this case, decimal format. If it can, it returns that number into the hourlyrate or hoursworked variable. If it can't it returns a zero.
decimal.tryparse(HourlyRateTextBox.Text, hourlyrate)
decimal.tryparse(HoursWorkedTextBox.Text, hoursworked)



There's a lot going on here that really needs some attention, you're not planning out your logic at all. It looks like you are frustrated and just tossing stuff in hoping it works.

Really think you need to sit down and take this step by step.
Get your logic planned, what steps should happen when in what procedure.
Once the planning is done then start coding.
Was This Post Helpful? 0
  • +
  • -

#7 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 01:57 PM

I dont want to get rid of everything that I have done so far. What would be the best thing to do and what should I delete?
Was This Post Helpful? 0
  • +
  • -

#8 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 02:04 PM

The problem is like I said earlier, you've injected logic in so many places that it is hard to keep track of what is where. Take my previous post, and go over your code. Get rid of things like the Salary() function, and make sure that the logic flow of your function/sub are correct. You will find that you're going to be removing/redoing a lot of what you have anyway which is why I advised you to just start over.

Let me see if this helps. I will help you build the function then you can do the sub.

In order to calculate wages earned you use the formula

Quote

(hoursWorked * hourlyWage) + ((hoursWorked - 40) * (hourlyWage * 0.5))

now if they are salaried then it is a straight 40 hours

Quote

(40 * hourlyWage)


So we know that we are going to need 3 inputs into our function. The hours worked, hourly wage and if they are salaried or not. Since we need this function to calculate the gross pay we will return a decimal value back from it


public Function Foo(ByVal a as Decimal, ByVal b as Decimal, ByVal c as Boolean) As Decimal

End Function 




to check if they are salaried or not we need to know if c is true or false. To do this we use an if statement:


' a is hours worked, b is hourly wage, and c is if they are salaried or not
public Function Foo(ByVal a as Decimal, ByVal b as Decimal, ByVal c as Boolean) As Decimal
    If (c) Then
    
    Else

    End If
End Function 




from there we now insert the appropriate calculation as a return into the if statement:
*Note: Since we need to calculate overtime if they worked more then 40 hours we need to check a to see if it is greater then 40. So we added another If statement for that check. Also you could do an Inline If statement, but in this case it is easier to read if you don't.

public Function Foo(ByVal a as Decimal, ByVal b as Decimal, ByVal c as Boolean) As Decimal
    If (c) Then
        Return 40 * b
    Else
        If a > 40 Then
            Return (a * B)/> + ((a - 40) * (b * 0.5))
        Else
            Return (a * B)/>
        End If
    End If
End Function 



That is all there is to creating the function. Now to call it we do the following:

Since you will be reusing them in multiple methods you will want class level variables (Variables defined right under the Class declaration) for hoursWorked, hourlyWage, and grossPay.

Public Class Form1
	Dim grossPay As Decimal = 0
	Dim hoursWorked As Decimal = 0
	Dim hourlyWage As Decimal = 0



now in the _Click event you will use the .TryParse method of Decimal to populate those variables from the textbox. (We use tryparse because if it fails it wont throw an error.)

Decimal.TryParse(TextBox1.Text, hoursWorked)
		Decimal.TryParse(TextBox2.Text, hourlyWage)



Next you will want to call the function to calculate the gross salary and pass the returned value to the textbox for gross salary

TextBox.Text = Foo(hoursWorked,hourlyWage,CheckBox.Checked).ToString()



Since we need to send in if they are salaried or not we will use the return value from the Checked property of the checkbox for the 3rd parameter.

Now since the .Text property of a textbox takes an input of Type string, and our Function returns a Type Decimal we need to convert it to string by calling .ToString() at the end of the function call.

I hope that my explination helps you. Try to create the sub and if you still have issues come back and we'll try and help you figure out what went wrong.

This post has been edited by rgfirefly24: 13 September 2012 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 02:05 PM

If you don't want to get rid of what you have written so far, you could always comment them out. The has the benefit of preserving what you have tried.

It's better to separate things into distinct and discreet pieces.

- Input
- Verification
Not every possible string is a valid parsing for a Number. Hence the suggest of .TryParse
- Calculation / Transformation / Processing.
Separating out the function from the form, make it easier of maintain, use and understand.
- Output

To me it's easier to start with a console application, rather than a form based one. As it focus you on the core aspects of the problem solution, rather than being distracted by controls and making it look pretty. That can be done latter.

This post has been edited by AdamSpeight2008: 13 September 2012 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Computing Weekly Gross Pay

Posted 13 September 2012 - 05:50 PM

another thing you can do with the tryparse is create a boolean variable to actually check if it was able to be converted or not.
so something like


dim num1 as decimal
dim num2 as decimal
dim isNum1 as boolean
dim isNum2 as boolean

' convert your textboxes here
isnum1 = decimal.tryparse(textbox1.text, num1)
isnum2 = decimal.tryparse(textbox2.text, num2)

' then use them in if statements

if isnum1 andalso isnum2
   do something
else
   messagebox.show("Check your entries and try again", "Error")



basically validating your data
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1