Help needed with straight-line depreciation

C Dull

Member
Joined
Jun 15, 2021
Messages
10
Programming Experience
Beginner
Hey guys,

As a beginner I'm going through exercises to try an learn the basics of C#. This exercise calls for a straight- line depreciation to be calculated with user entering inputs for present value, salvage value, and years of depreciation. I'm stuck at this point and don't know how I should proceed -- if anyone can review and provide me with some direction it would be appreciated!

Straightline Depreciation Calculator:
namespace StraightlineDepreciationCalculator
{
    public partial class frmStraightlinedDepreciationCalculator : Form
    {
        public frmStraightlinedDepreciationCalculator()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Clears text boxes and selects first tab in form
            txtPresentValue.Text = "";
            txtSalvageValue.Text = "";
            txtYearsOfDepreciation.Text = "";
            txtDisplay.Text = "";
            txtPresentValue.SelectAll();
            txtPresentValue.Select();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // This function confirms and exits the application
            DialogResult dlgresult;
            dlgresult = MessageBox.Show("Do you want to exit the application?", "Close the form", MessageBoxButtons.YesNo);
            if (dlgresult == DialogResult.Yes)
            {
                //Closes the application
                System.Environment.Exit(1);
            }
            else
            {
                txtPresentValue.Select();
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            // Validates user inputted data and calculates the value at the end of each year
            // displayed in multiline textbox
            double dblPresentValue = 0;
            double dblSalvageValue = 0;
            int intYearsOfDepreciation = 0;
            int intCounter = 0;
            string strDisplay = "";

            // Clear any text already in display
            txtDisplay.Text = "";

            // Get values from text boxes
            dblPresentValue = Convert.ToDouble(txtPresentValue.Text);
            dblSalvageValue = Convert.ToDouble(txtSalvageValue.Text);
            intYearsOfDepreciation = Convert.ToInt32(txtYearsOfDepreciation.Text);

            // Validate user entry
            if (dblPresentValue < 1 | dblPresentValue > 1000000)
            {
                MessageBox.Show("Present value must be between $1 and $1,000,000");
                txtPresentValue.Select(); //selects defective input text box
                txtPresentValue.SelectAll();
            }
            else if (dblSalvageValue > dblPresentValue)
            {
                MessageBox.Show("Salvage value cannot be greater than Present Value");
                txtSalvageValue.Select(); //selects defective input text box
                txtSalvageValue.SelectAll();
            }
            else if (intYearsOfDepreciation < 0 | intYearsOfDepreciation > 25)
            {
                MessageBox.Show("Years of Depreciation must be in 1 - 25 range");
                txtYearsOfDepreciation.Select(); //selects defective input text box
                txtYearsOfDepreciation.SelectAll();
            }
            else
            {
                // set up column headers in text display
                strDisplay = "Year      Asset Value/r/n-------      ---------------";

            }
                

        }
    }
}
 

C Dull

Member
Joined
Jun 15, 2021
Messages
10
Programming Experience
Beginner
getting it to work using this, just need to format for currency. Disregard.

C#:
 strDisplay = "Year      Asset Value\r\n-------      ---------------";
                // pre-test loop
                double period = (dblPresentValue - dblSalvageValue)/intYearsOfDepreciation;

                int curYear = DateTime.Now.Year;
                while (intCounter < intYearsOfDepreciation) //loop control statement
                {           
                    strDisplay += "\r\n"+ curYear.ToString() + ": " + dblPresentValue.ToString();
                    intCounter = intCounter + 1;
                    dblPresentValue -= period;
                    curYear++;
                }
                txtDisplay.Text = strDisplay;
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,555
Location
Chesapeake, VA
Programming Experience
10+
If you are dealing with money, you should be using decimal, not double.

As for formatting:
 
Top Bottom