How can I properly use the Trace.Write() method inside of my for loop to print month

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dseals22
    New Member
    • Jan 2017
    • 74

    How can I properly use the Trace.Write() method inside of my for loop to print month

    I am struggling with trying to figure out how I can output a trace message in ASP.NET using the Trace.Write() method inside of a for loop in order for a user to see the future value after 1 month, 10 months, and 50 months? The format should be like this:
    Month{i}: & Value{futureVal ue}. So, for example, the format should look like this after the first month: "Month:" {i} "Value:"{future Value}. i know that's not right but that's why I need help understanding how I can input the variables correctly into this method. I have researched my problem, but it's not specific to my individual problem. Here is the site I have researched before coming here: (http://www.c-sharpcorner.com/UploadFile/225740/how-to-write-custom-tracing-message-in-Asp-Net/)

    Code:
    namespace XEx05FutureValue
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    for (int i = 50; i <= 500; i += 50)
                        ddlMonthlyInvestment.Items.Add(i.ToString());
            }
    *
            protected void btnCalculate_Click(object sender, EventArgs e)
            {
                if (IsValid)
                {
                    int monthlyInvestment = Convert.ToInt32(ddlMonthlyInvestment.SelectedValue);
                    decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text);
                    int years = Convert.ToInt32(txtYears.Text);
    *
                    decimal futureValue = this.CalculateFutureValue(monthlyInvestment,
                        yearlyInterestRate, years);
    *
                    lblFutureValue.Text = futureValue.ToString("c");
                }
            }
    *
            protected decimal CalculateFutureValue(int monthlyInvestment,
            decimal yearlyInterestRate, int years)
            {
                int months = years * 12;
                decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;
                decimal futureValue = 0;
                for (int i = 0; i < months; i++)
                {
                    futureValue = (futureValue + monthlyInvestment)
                        * (1 + monthlyInterestRate);
                    if (Trace.IsEnabled)
                    {
                        Trace.Write({i} {futureValue});
                    }
                }
                return futureValue;
            }
    *
            protected void btnClear_Click(object sender, EventArgs e)
            {
                ddlMonthlyInvestment.SelectedIndex = 0;
                txtInterestRate.Text = "";
                txtYears.Text = "";
                lblFutureValue.Text = "";
            }
        }
    }
Working...