# Does anyone understand and could help me understand VB 2010

• ### Question

• I am trying to do homework for Programming class, however I'm very confused.  I am working on chapter 6 case programming assignment #6.

totally failed #5 and still very confused.  Please someone help.......Thanks

Lori44

Lori

Friday, March 23, 2012 6:21 PM

• I need to find the annual salary from the hourly rate, so it would be rate \$20 *40 hrs * 52 weeks.  then the percentage of increase

You have already checked the annual salary calculation, because that is the figure you use for the first year:

```        For YearNumber As Integer = 1 To 10
If YearNumber = 1 Then
```

You need to find the amount of the increase, not the percentage, for the subsequent years. You already have the percentage increase from the textbox entry.

So the maths calculation you need will use an amount and a percentage to calculate the increase in that amount.  That's the calculation you need to put in the GetAnnualPayIncrease method, so you can calculate and display the next year's salary, using the code you already have:

```             Else
AnnualSalary += GetAnnualPayIncrease(AnnualSalary, Rate)
Me.lstYearlyPay.Items.Add("Year" & YearNumber & ": " & AnnualSalary.ToString("C"))

End If

Next
```
Saturday, March 24, 2012 12:17 AM

### All replies

• You'll get a better response if you give some indication of what problems you are having.  For example you could post the code you have tried that doesn't work.

Friday, March 23, 2012 6:37 PM
• ok here goes..  The only thing that works correctly is the clear and exit when I   click on the next decade pay button it comes up with high numbers (Which are wrong and it also adds a 0 1 after the 10 years)

Option Strict On

Private Sub btnCalculagtePayForNext10Years_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatePayForNext10Years.Click
'The btnCalculatePayForNext10Years click event calculates the yearly pay based on 40 hours per week and 52 weeks per year. The raise increases each amount after the first year.

'Declare and initialize the variables.

Dim HourlyPay As Integer = CInt(txtPresentPayPerHour.Text)
Dim Rate As Decimal = CDec(txtPercentagePerYear.Text)
Dim AnnualSalary As Decimal = ((HourlyPay * 40) * 52)

Dim strPresentPayPerHour As String = Me.txtPresentPayPerHour.Text
Dim decPresentPayPerHour As Decimal
Dim strPercentagePerYear As String = Me.txtPercentagePerYear.Text
Dim decPercentagePerYear As Decimal
Dim strTotalPay As String
Dim decTotalPay As Decimal = CDec(CStr(decPresentPayPerHour * 40 * 52 * decPercentagePerYear))
Dim intYears As Integer
Dim strNormalBoxMessage As String = "Enter the present pay and expected raise"
Dim strNonNumericErrorMessage As String = "Error - Enter a number for the present pay and expected raise"
Dim strNegativeNumberErrorMessage As String = "Error - Enter a positive #"

'Declare and initialize loop variables.

'Dim StrCancelButtonClicked As String = ""

For YearNumber As Integer = 1 To 10
If YearNumber = 1 Then

Else
AnnualSalary += GetAnnualPayIncrease(AnnualSalary, Rate)
Me.lstYearlyPay.Items.Add("Year" & YearNumber & ": " & AnnualSalary.ToString("C"))

End If

Next

'For intYear = 1 to 10
'   decTotalPay = (decPresentPayPerHour * decPercentagePerYear)
'   Me.lstYearlyPay.Item.Add("Year" & intYears & "Total Pay" & _decTotalPay)
'Next

'The loop allows the user's present pay and expected raise to be calculated to estimate their pay for the next ten years.
'The loop terminates when it has calculated ten years or the user clicks the cancel button.

strTotalPay = CStr(decTotalPay)

'Do Until intYears = 10 or strTotalPay = CtrCancelButtonClicked

'Loop
If IsNumeric(decPresentPayPerHour) Then
decPresentPayPerHour = Convert.ToDecimal(decPresentPayPerHour)
End If

If IsNumeric(decPercentagePerYear) Then
decPercentagePerYear = Convert.ToDecimal(decPercentagePerYear)
End If

If decPresentPayPerHour >= 0 Then
decTotalPay += decPresentPayPerHour * decPercentagePerYear
intYears += 1
strPresentPayPerHour = strNormalBoxMessage
End If

If decPresentPayPerHour <= 0 Then
strPresentPayPerHour = strNonNumericErrorMessage
End If

If decPresentPayPerHour <= 0 Then
strPresentPayPerHour = strNegativeNumberErrorMessage
End If

If decPercentagePerYear >= 0 Then
decTotalPay += decPresentPayPerHour * decPercentagePerYear
intYears += 1
strPresentPayPerHour = strNormalBoxMessage
End If

If decPercentagePerYear <= 0 Then
strPercentagePerYear = strNonNumericErrorMessage
End If

If decPercentagePerYear <= 0 Then
strPercentagePerYear = strNegativeNumberErrorMessage
End If

'Makes List Visible

Me.lstYearlyPay.Visible = True

'Calculate and display ten years of pay
If intYears > 1 Then
decTotalPay = decPresentPayPerHour * decPercentagePerYear
Me.lstYearlyPay.Text = "Pay for the next ten years is " & _
decTotalPay.ToString("C0")
Else
'Display error message if user enters NonNumeric or Negative Numbers.

Me.lstYearlyPay.Text = "No Pay or Raise Entered"
MsgBox("You Entered ", , "Error - Enter a number for the present pay and expected raise")
MsgBox("Enter a Negative number ", , "Error - Enter a positive #")
MsgBox("You Entered ", , "Error - Enter a number for the present pay and expected raise")
MsgBox("Enter a Negative number ", , "Error - Enter a positive #")

End If

'Disable the Present Pay Per Hour and Percentage Per Year buttons.

Me.txtPresentPayPerHour.Enabled = False
Me.txtPercentagePerYear.Enabled = False

End Sub
Private Function GetAnnualPayIncrease(ByVal AnnualPay As Decimal, ByVal Rate As Decimal) As Decimal
Dim SalaryIncrease As Decimal = AnnualPay + (Rate / 100)
Return SalaryIncrease

End Function
Private Sub mnuClearItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClearItem.Click
'This mnuClearItem click event clears the ListBox object, Enter Pay text Box and clears the Enter Raise text box.
'It also enables the Enter Present Pay Per Hour button and Enter Percentage Per Year Button.

Me.lstYearlyPay.Items.Clear()
Me.txtPresentPayPerHour.Enabled = True
Me.txtPercentagePerYear.Enabled = True
Me.txtPresentPayPerHour.Clear()
Me.txtPercentagePerYear.Clear()

End Sub
Private Sub mnuExitWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExitWindow.Click
'This Exit Window button click event closed the program and exits the application.

Close()

End Sub

End Class

• Edited by Friday, March 23, 2012 6:55 PM
Friday, March 23, 2012 6:43 PM
• This is what its looks like.........\$20 per hour with a 10% increase

Lori

Friday, March 23, 2012 6:59 PM
• Can you show us the wording of the actual assignment?

Your code comments indicate that you have a "cancel" button for the processing of a loop - that is a pretty advanced technique not easily implemented for a beginner when done correctly (there is a cheap trick you could use, but no one should be teaching it in a modern class and I don't see it in your code anyway).  I mention this because I'm wondering if you've tried to fancy-up the assignment and have perhaps done yourself a disfavor in doing so.

If this assignment instructs you to provide a cancel button, then someone needs slapped for putting that in chapter 6!  =P

Here's a few pointers for getting help on these forums:

Use the Insert Code button - up in the menu bar of the post editor, there are buttons for insterting html, code, and images.  When you use the insert code button you can select the language using a dropdown and the code will get formatted nicely in its own scrollbox in the post.  Makes it much easier to read.

For homework questions, try to keep each post very narrow and specific.  Focus on one problem at a time and rather than post your whole project, just include the relevant code.  Try to include a good description of what you've been asked to do, how you thought to go about doing it, and what you've actually tried so far.  Then you can explain the part that confuses you.

Right now, I'm not quite sure where you are stuck at...

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Friday, March 23, 2012 8:54 PM
• Your loop to calculate and display the ten items is here:

```        For YearNumber As Integer = 1 To 10
If YearNumber = 1 Then

Else
AnnualSalary += GetAnnualPayIncrease(AnnualSalary, Rate)
Me.lstYearlyPay.Items.Add("Year" & YearNumber & ": " & AnnualSalary.ToString("C"))

End If

Next
```

Your error messages follow that loop when they should be before.  You repeat some of that calculation (incorrectly) in the code that follows that loop.

Your subroutine to calculate the annual increase is returning the value AnnualPay + (Rate / 100). You are adding this return value to the annual salary, so the result is
AnnualSalary = Annual Salary + AnnualSalary + Rate / 100)

or  41,600 + 41,600 + (10/100)  or 83,2000.10.  If the function really should be GetAnnualPayIncrease then you need to calculate the amount that the annual pay increased during that year, not the figure that it increased to.

The code in this line
Dim decTotalPay As Decimal = CDec(CStr(decPresentPayPerHour * 40 * 52 * decPercentagePerYear))

has two unnecesssary conversions.

Friday, March 23, 2012 9:10 PM
• I get what your saying and I can do it on Excel but can't figure it out on this.

Lori

Friday, March 23, 2012 11:08 PM
• What was the result when you made the change I suggested?  To work out your calculation for the annual pay increase you need to be able to express it as a mathematical formula.   You must have that formula correct before you consider trying to write any code.

Friday, March 23, 2012 11:19 PM
• I change it code but its its still doing same, now sure what else to do and I don't understand this

Lori

Friday, March 23, 2012 11:31 PM
• I need to find the annual salary from the hourly rate, so it would be rate \$20 *40 hrs * 52 weeks.  then the percentage of increase,

Lori

Friday, March 23, 2012 11:35 PM
• I need to find the annual salary from the hourly rate, so it would be rate \$20 *40 hrs * 52 weeks.  then the percentage of increase

You have already checked the annual salary calculation, because that is the figure you use for the first year:

```        For YearNumber As Integer = 1 To 10
If YearNumber = 1 Then
```

You need to find the amount of the increase, not the percentage, for the subsequent years. You already have the percentage increase from the textbox entry.

So the maths calculation you need will use an amount and a percentage to calculate the increase in that amount.  That's the calculation you need to put in the GetAnnualPayIncrease method, so you can calculate and display the next year's salary, using the code you already have:

```             Else
AnnualSalary += GetAnnualPayIncrease(AnnualSalary, Rate)
Me.lstYearlyPay.Items.Add("Year" & YearNumber & ": " & AnnualSalary.ToString("C"))

End If

Next
```
Saturday, March 24, 2012 12:17 AM
• Ok But how

Lori

Saturday, March 24, 2012 1:06 AM
• That's a math question, not a VB question.  For math questions there are many resources available on the net - here's one that apllies specifically to percentage calculations:
http://www.mathsisfun.com/percentage.html

When you know the formula you want to use, it can be expressed as VB code.  But you need to know the formula first.

Saturday, March 24, 2012 2:22 AM
• 41600 plus 10% = 4160  (this would be \$20 per hour * 40 hours * 52 weeks = 41600)

Because 10% = 10/100
So you are adding (10/100) x 41600 = 4160
And 41600 + 4160 = 45760

So is this the formula I need and If so how would I express it as a VB code?

Lori

Saturday, March 24, 2012 2:43 AM
• 41600 plus 10% = 4160

No it doesn't.   41600 plus 10% would be the amount of the 2nd year salary.  The figure you quote is the increase, not the new amount. That's where the confusion lies.

To calculate the increase you take 10% of 41600.

So you are adding (10/100) x 41600 = 4160

Yes.  That is the increase you would add to the previous years amount.

And 41600 + 4160 = 45760

So you will add the increase to the previous year to get the current year.  This code will do that:
AnnualSalary += GetAnnualPayIncrease(AnnualSalary, Rate)
if GetAnnualPayIncrease returns the increase, not the new amount.    This code is wrong:
Private Function GetAnnualPayIncrease(ByVal AnnualPay As Decimal, ByVal Rate As Decimal) As Decimal
Dim SalaryIncrease As Decimal = AnnualPay + (Rate / 100)
Return SalaryIncrease

That middle line needs a formula to calculate the increase for the year ('SalaryIncrease') using the previous year annual salary ('AnnualPay') and the rate ('Rate'). You need to compare that line to the calculation you used to get 4160 (above) and change the line to match the formula.

Saturday, March 24, 2012 3:40 AM
• OK, get what your saying but don't get how to word it in VB

Lori

Saturday, March 24, 2012 4:16 AM
• What exactly is the difficulty you are having in wording it for VB?  You need to show exactly what you are trying to do, what you have attempted, what was the result, and where you think it might be wrong.

Saturday, March 24, 2012 4:47 AM
• That's just it I don't know where I went wrong...

Lori

Saturday, March 24, 2012 4:57 AM
• will u be around tomorrow? I have to god to bed I work in the am 8 to about 1 cst, sorry being a pain but you are helping me, so thank you.

Lori

Saturday, March 24, 2012 5:02 AM
• Hi Lori44,

Welcome to the MSDN forum.

Have you solve your issue with the help of members? As Acamar said, if you also confused with this issue, you can post the code you have done and told us why it is wrong, the result is wrong or have exception.  We will help you to solve your issue.

Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us

Monday, March 26, 2012 9:23 AM
• Acmar found your issue, it lies with the GetAnnualPayIncrease function.  You were trying to add 10%, but instead told the computer to add \$0.10, which is why you'll see your answers are doubling and adding a dime each year.  You were trying to add the percenatge, but you need to multiply by it.

Change the following code to the function below it.

```    Private Function GetAnnualPayIncrease(ByVal AnnualPay As Decimal, ByVal Rate As Decimal) As Decimal
Dim SalaryIncrease As Decimal = AnnualPay + (Rate / 100) 'Here is where you went wrong.  The addition should be a multiply.
Return SalaryIncrease
End Function

Private Function GetAnnualPayIncrease(ByVal AnnualPay As Decimal, ByVal Rate As Decimal) As Decimal
Dim SalaryIncrease As Decimal = AnnualPay * (Rate / 100)
Return SalaryIncrease
End Function

```

Lines to Code by: Hardware can only do what it's told. That's why there are programmers. ** Just because you don't know how, doesn't mean it can't be done. ** When something doesn't work, it's usually a programming error. ** Just because you did something last time, is not a good enough reason to do it this time. ** If the user can do it, at some point they will. When they do, it's the programmer's fault. ** Keep your code modular. Someone else will have to read it at some point. ** Add comments. In six months, you won't know why you wrote the code like that.

Monday, March 26, 2012 3:47 PM