Below code is working fine with small problem. My problem here is, how can i get the BegValue working in a proper order. please it is only the begvalue that is giving me a headache. Please some one help me. !!!

Thanks in advance.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Cost, Scrap, Life, Period, BegValue, EndValue, Deprec, AccumDeprec, YearPurch As Double
    Dim Fmt As String = "###,##0.00"

    Cost = txtcost.Text
    Scrap = txtscrap.Text
    Life = txtlife.Text
    YearPurch = txtyear.Text
    Period = txtlife.Text



    DataGridView1.ColumnCount = 6
    DataGridView1.Columns(0).Name = "Begin Year"
    DataGridView1.Columns(1).Name = "Begin Value"
    DataGridView1.Columns(2).Name = "Depreciation"
    DataGridView1.Columns(3).Name = "Accum.Depreciation"
    DataGridView1.Columns(4).Name = "End Value"
    DataGridView1.Columns(5).Name = "End Year"

    DataGridView1.Rows.Clear()

    For Period = 1 To Life

        BegValue = Cost
        Deprec = SYD(Cost, Scrap, Life, Period)
        AccumDeprec += Deprec
        EndValue = BegValue - Deprec

        Dim row As String() = New String() {YearPurch, _
                                             FormatNumber(BegValue), _
                                             FormatNumber(Deprec), _
                                             FormatNumber(AccumDeprec), _
                                             FormatNumber(EndValue), _
                                             YearPurch}



        DataGridView1.Rows.Add(row)

        BegValue = EndValue

        If Period = Life - 1 Then

            Deprec = BegValue - Scrap

        Else

            Deprec = SYD(Cost, Scrap, Life, Period)

        End If

        YearPurch += 1

    Next

End Sub

End Class

Dani AI

Generated

The root cause was that BegValue kept being reset to the original cost each iteration, so every row showed the same beginning book value. already fixed that by moving the initial BegValue = Cost outside the loop. Two further, commonly missed points worth applying:

  • compute each period's depreciation before you update accumulators or write the row (so the displayed row reflects the values for that period);
  • force the last period to take whatever remains down to salvage (this handles rounding differences).

A compact, correct SYD implementation and a clean loop pattern (using Decimal for money and Integer for counters) will make the output stable and easy to follow.

Function SYD(cost As Decimal, salvage As Decimal, life As Integer, period As Integer) As Decimal
    Dim totalYears As Integer = life * (life + 1) \ 2
    If totalYears = 0 Then Return 0D
    Dim remaining As Integer = life - period + 1
    Return (cost - salvage) * remaining / totalYears
End Function
' parse and validate inputs first (use TryParse in production)
Dim cost As Decimal = Decimal.Parse(TxtCost.Text)
Dim salvage As Decimal = Decimal.Parse(txtscrap.Text)
Dim life As Integer = Integer.Parse(TxtLife.Text)
Dim year As Integer = Integer.Parse(txtYear.Text)

Dim begValue As Decimal = cost
Dim accumDep As Decimal = 0D

For p As Integer = 1 To life
    Dim deprec As Decimal
    If p = life Then
        ' last period — force remaining book value down to salvage
        deprec = Math.Max(0D, begValue - salvage)
    Else
        deprec = SYD(cost, salvage, life, p)
    End If

    accumDep += deprec
    Dim endValue As Decimal = begValue - deprec

    ' add numeric/formatted values to grid (store numbers where possible)
    DataGridView1.Rows.Add(year, begValue.ToString("N2"), deprec.ToString("N2"),
                           accumDep.ToString("N2"), endValue.ToString("N2"), year)

    begValue = endValue
    year += 1
Next

Brief tips: enable Option Strict On to avoid implicit conversions; use Decimal for currency; initialize accumulators (accumDep = 0D) before the loop; validate TextBox input with TryParse; and avoid recalculating deprec after you already added the row (that was the logic bug left in the earlier code). These small changes ensure SYD values and the running book value behave correctly across every period.

Recommended Answers

All 3 Replies

What is the problem that you are having? What is the formula that you are using to calculate the values?

What I think is that move all the calculation above the "line 31. Dim row As String
() = New String()" because what I see is that before you calculate the BegValue you add everything to a database and the just below or after you added things to the database you then calculate the BagValue so as you save it, it has the Cost values so to get the correct results also try to remove this assign BegValue = Cost just use cost straight there because you used twice begvalue and only use it here:" line 42. BegValue =
EndValue" and see.

I was able to solve it yesterday by my self and thank you for your advice.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Cost, Scrap, Life, Period, BegValue, EndValue, Deprec, AccumDeprec, YearPurch As Double
    Dim Fmt As String = "###,##0.00"

    Cost = TxtCost.Text
    Scrap = txtscrap.Text
    Life = TxtLife.Text
    YearPurch = txtYear.Text
    Period = TxtLife.Text



    DataGridView1.ColumnCount = 6
    DataGridView1.Columns(0).Name = "Begin Year"
    DataGridView1.Columns(1).Name = "Begin Value"
    DataGridView1.Columns(2).Name = "Depreciation"
    DataGridView1.Columns(3).Name = "Accum.Depreciation"
    DataGridView1.Columns(4).Name = "End Value"
    DataGridView1.Columns(5).Name = "End Year"

    DataGridView1.Rows.Clear()

    BegValue = Cost

    For Period = 1 To Life

        Deprec = SYD(Cost, Scrap, Life, Period)
        AccumDeprec += Deprec
        EndValue = BegValue - Deprec

        Dim row As String() = New String() {YearPurch, _
                                             FormatNumber(BegValue), _
                                             FormatNumber(Deprec), _
                                             FormatNumber(AccumDeprec), _
                                             FormatNumber(EndValue), _
                                             YearPurch}



        DataGridView1.Rows.Add(row)

        BegValue = EndValue


        If Period = Life - 1 Then

            Deprec = BegValue - Scrap

        Else

            Deprec = SYD(Cost, Scrap, Life, Period)

        End If

        YearPurch += 1

    Next

End Sub

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.