Need help with Calculation

WackoWolf

Active member
Joined
Jan 6, 2006
Messages
39
Location
Rhode Island
Programming Experience
Beginner
I have a form with a scrollbar that when click will show the amount of years in a textbox. The scrollbar works fine, but the calculation comes out wrong when i try it for 3 years. The formulas for the project is:
1. Yearly stright line decpreciation = (cost - salvage value)/5
2. Double declining depreciation = .4 * (cost - previous years depreciation)
3. Sum of the years digits depreciation = (6 - years)/15 * cost
Now when I do it for 3 years the declining balance depreciation should be 144 if the cost of the item is 1000, and the salvage is 100. The answer I get for the decliniing balance is 328 not 144. The depreciation is 200 which is right. Could someone tell me were my calculation is wrong, and what I need to do to fix it. I can't use a loop. Here is my code. Please help, been messing with this since Friday night.

Thank You
WackoWolf

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SalvageValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Year [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StraightLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PreviousYear [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Declining [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Sum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
 
[/COLOR][/SIZE][SIZE=2]ItemCost = TXTItemCost.Text
SalvageValue = TXTSalvageValue.Text
Year = TXTYear.Text
 
StraightLine = (ItemCost - SalvageValue) / 5
Declining = 0.4 * (ItemCost - StraightLine)
Sum = (6 - Year) / 15 * ItemCost
TXTStraightLine.Text = StraightLine
TXTDecliningBalance.Text = Declining
TXTDepreciationSum.Text = Sum
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
That is what it shows in the book for 3 years. It should coume out to 144.
Strightline = 180
declinning balance = 144
sum of the years = 200

This is what the book has:

1. Yearly stright line decpreciation = (cost - salvage value)/5
2. Double declining depreciation = .4 * (cost - previous years depreciation)
3. Sum of the years digits depreciation = (6 - years)/15 * cost
 
2. Double declining depreciation = .4 * (cost - previous years depreciation)

what is the previous years depreciation? where is this value comming from?
 
The book doesn't say, that all that it shows.

StrightLine Depreciation is 180
Declinning Balance Depreciation is 144
Sum of the Years Digits Depreciation is 200

Thats all there is in the book

"it says to use straight line, declining balance, and sum of the years digits depreciation methods."
 
ok, i got it, i had things in the wrong order
Don Delegate's post was correct and it makes sense now

i just put everything into a button but this is it:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ItemCost As Decimal = CDec(txtItemCost.Text)
        Dim SalvageValue As Decimal = CDec(txtSalvageValue.Text)
        Dim Years As Decimal = CDec(txtYears.Text)

        Dim StraightLine As Decimal
        Dim DecliningBalance As Decimal
        Dim SumOfYears As Decimal

        StraightLine = (ItemCost - SalvageValue) / 5
        SumOfYears = ((6 - Years) / 15) * ItemCost
        For Year As Int32 = 1 To CInt(Years)
            DecliningBalance = CDec(0.4 * ItemCost)
            ItemCost -= DecliningBalance
        Next Year

        lblStraightLine.Text = StraightLine.ToString
        lblDecliningBalance.Text = DecliningBalance.ToString
        lblSumOfYears.Text = SumOfYears.ToString
    End Sub
 
I am useing a scroolbar, not sure if that will cause a problem it shouldnt'

I am useing txt boxs for the out put and the last 3 lines of code doesn't put the output to the txt boxs. The names for the TXTBoxs are:
1. TXTDepreciation
2. TXTStraightLine
3. TXTDeclinningBalance

Every way I try to change thoses lines to make it go to the TXTBoxs it doesn't work. Nothing goes into any of the TXTBoxes, Can you help one more time with this. If you want I can zip the project up and send it to you and you can see if it will work for you. Let me Know.

Thank You
WackoWolf
 
to adapt it, just use the Scrollbar's value for the years (obviously the code will be in the scrollbar event, not a button)


and you can use textbox's instead of labels (i used labels) so obviously it'd be:
TXTDepreciation instead of lblSumOfYears
TXTStraightLine instead of lblDecliningBalance
TXTDeclinningBalance instead of lblDeclinningBalance
 
I have been trying stuff, and I just notice that you replied back. I got it to work to a certain part. I am haveing a problem getting TXTDeclinning.Text to declair. I am posting the code that I now have, the last line of code is rem out but that is where the problem is.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Declinning [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StraightLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtYears [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DeclinningBalance [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTItemCost.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SalvageValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTSalvageValue.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Years [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](txtYears) 
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SumOfYears [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Sum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]StraightLine = (ItemCost - SalvageValue) / 5
SumOfYears = ((6 - Years) / 15) * ItemCost
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] Year [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32 = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](Years)
DeclinningBalance = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](0.4 * ItemCost)
ItemCost = DeclinningBalance
 
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] Year
TXTStraightLine.Text = StraightLine
TXTDepreciationSum.Text = Sum
 
' This is the line that will not declair
 
' TXTDeclinningBalance.Text = Declinning
[/SIZE]
 
That's not a declaration. It's an assignment. What does the error message say? Strictly speaking you should be converting a Decimal to a String before displaying it in a TextBox but you've done the same in the previous lines so obviously you have Option Strict Off and the conversion is happening implicitly. The only other possiblity that I can think of is that you have no TextBox with that name.

Can I also point out this anomoly:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] [I][B][U]txtYears[/U][/B][/I] [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DeclinningBalance [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTItemCost.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SalvageValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTSalvageValue.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Years [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2]([I][B][U]txtYears[/U][/B][/I]) [/SIZE]
You are declaring the 'txtYears' variable, then without ever assigning a value to it you are assigning its value to the 'Years' varaible. That does not make sense.
 
I see your point. I am now pasteing the code I am trying to use. I get the first box to read, but the second one doesn't give a output and the last gives a zero.

VB.NET:
[SIZE=2][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Declinning [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StraightLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SumOfYears [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DeclinningBalance [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTItemCost.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SalvageValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTSalvageValue.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Years [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Sum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]StraightLine = (ItemCost - SalvageValue) / 5
SumOfYears = ((6 - Years) / 15) * ItemCost
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] Year [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32 = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](Years)
DeclinningBalance = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](0.4 * ItemCost)
ItemCost = DeclinningBalance
 
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] Year
 
 
TXTStraightLine.Text = StraightLine.ToString
[/SIZE][SIZE=2][COLOR=#008000]'TXTDecliningBalance.Text = DecliningBalance.ToString
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' TXTSumOfYears.Text = SumOfYears.ToString
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'TXTStraightLine.Text = StraightLine
[/COLOR][/SIZE][SIZE=2]TXTDepreciationSum.Text = Sum
[/SIZE][SIZE=2][COLOR=#008000]' TXTDeclinningBalance.Text = Declinning[/COLOR][/SIZE]
 
[/SIZE]

 
I now have it were I don't have any errors, I made sure the boxes are the right name. That was a problem. The problem is that I only get a value in the first box which is 180. The other 2 boxes show a zero. What could be wrong now? This is the code I now have.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Declinning [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StraightLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SumOfYears [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DeclinningBalance [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemCost [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTItemCost.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SalvageValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](TXTSalvageValue.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Years [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Sum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]StraightLine = (ItemCost - SalvageValue) / 5
SumOfYears = ((6 - Years) / 15) * ItemCost
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] Year [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32 = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](Years)
DeclinningBalance = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2](0.4 * ItemCost)
ItemCost = DeclinningBalance
 
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] Year
 
TXTDepreciationSum.Text = Sum
TXTStraightLine.Text = StraightLine.ToString
TXTDeclinningBalance.Text = DeclinningBalance.ToString
 
[/SIZE]

 
One small point: declining is not double-n.

As for your calculations, you should use the debugger to trace your code. Set a breakpoint on the first line. To do that either click the line and press F9 or click beside the line in the left-hand margin. Then when execution breaks at that point you can step through the code line by line using F10. You can use the Autos, Locals and Watch windows to check the values of variables and properties and to evaluate expressions. You can also mouse-over variables and properties to see what they contain. Before each step you should ask yourself what you expect to happen. Then you press F10 and see if what you expected actually did happen. if it did then you keep going. If it didn't then you've found an issue. Either the code is wrong or you're expecting the wrong thing. Fix the issue and start again. keep going until you can get right to the end without an issue and you're done. Easy!
 
Back
Top