Loop Problems

wilsonwerks
05-02-2001, 06:25 PM
I have three problems with the following loop program:
1) How do I do I print string info before each loop value? (eg.: "After two years"; value, "After three years"; value, etc.)
2) I'm losing the original value of "cost" because the first loop redifines it as zero. I tried putting "ByVal cost" in the click event and DoubleDeclining sub procedure but it didn't work.
3) DoubleDeclining needs to depreciate 2/nths of the new value every year, not depreciate the same amount every year.
Until the last year when it wipes out the remaing balance.
The user defines the number of years to be depreciated.
Option Explicit

Private Sub cmdCalculate_Click()
Dim item As String, year As String
Dim cost As Single, number As Integer
Dim depreciation As Single, n As Integer
Call InputData(item, year, cost, number)
Call StraightLine(item, year, cost, number, depreciation)
Call DoubleDeclining(item, year, cost, number, depreciation)
End Sub

Private Sub InputData(item As String, year As String, cost As Single, number As Integer)
item = txtItem.Text
year = txtYear.Text
number = Val(txtNumber.Text)
cost = Val(txtCost.Text)
End Sub

Private Sub StraightLine(item As String, year As String, cost As Single, number As Integer, depreciation As Single)
'Calculate Straight Line Depreciation
depreciation = cost * (1 * (1 / number))
picDisplay.Print "The Straight Line Depreciation Schedule for your"
picDisplay.Print item; ", purchased in the year "; year; " at the"
picDisplay.Print "cost of"; cost; "is as follows:"
picDisplay.Print "The value after one year is:";
Do While cost > 0
cost = cost - depreciation
picDisplay.Print cost
Loop
End Sub

Private Sub DoubleDeclining(item As String, year As String, cost As Single, number As Integer, depreciation As Single)
'Calculate Straight Line Depreciation
depreciation = cost * (1 * (2 / number))
picDisplay.Print "The Double Declining Balance Depreciation Schedule"
picDisplay.Print "for your "; item; ", purchased in the year "; year; "at the"
picDisplay.Print "cost of"; cost; "is as follows:"
picDisplay.Print "The value after one year is:";
Do While cost > depreciation
cost = cost - depreciation
picDisplay.Print cost
Loop

End Sub

"I drink, therefore I am." -Rene Descartes

MelissaEvans
05-02-2001, 07:26 PM
See if this fixes it. =)

Private Sub StraightLine(item As String, year As String, cost As Single, number As Integer, depreciation As Single)
Dim ThisYear as Integer ' for calculating the current year in the loop - problem #1
Dim ThisCost as Single ' for changing local copy of the cost - problem #2
ThisCost = Cost ' use the local variable so it doesn't affect anything else - problem #2
'Calculate Straight Line Depreciation
depreciation = cost * (1 * (1 / number))
picDisplay.Print "The Straight Line Depreciation Schedule for your"
picDisplay.Print item; ", purchased in the year "; year; " at the"
picDisplay.Print "cost of"; cost; "is as follows:"
Do While cost > 0
picDisplay.Print "The value after " & ThisYear & " year is:"; ' moved to inside the loop - prolem #1
ThisCost = ThisCost - depreciation ' changed to ThisCost - problem #2
picDisplay.Print cost
ThisYear = ThisYear + 1 ' incriment year for the next iteration - problem #1
Loop
End Sub

Private Sub DoubleDeclining(item As String, year As String, cost As Single, number As Integer, depreciation As Single)
Dim ThisCost as Singls ' same purpose as above
ThisCost = cost
'Calculate Straight Line Depreciation
picDisplay.Print "The Double Declining Balance Depreciation Schedule"
picDisplay.Print "for your "; item; ", purchased in the year "; year; "at the"
picDisplay.Print "cost of"; cost; "is as follows:"
picDisplay.Print "The value after one year is:";
Do While cost > depreciation
depreciation = ThisCost * (1 * (2 / number)) ' moved inside the loop, now uses the new cost each time - problem #3
ThisCost = ThisCost - depreciation
picDisplay.Print cost
Loop
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum