By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,867 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,867 IT Pros & Developers. It's quick & easy.

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

P: 74
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{futureValue}. So, for example, the format should look like this after the first month: "Month:" {i} "Value:"{futureValue}. 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/)

Expand|Select|Wrap|Line Numbers
  1. namespace XEx05FutureValue
  2. {
  3.     public partial class Default : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             if (!IsPostBack)
  8.                 for (int i = 50; i <= 500; i += 50)
  9.                     ddlMonthlyInvestment.Items.Add(i.ToString());
  10.         }
  11. *
  12.         protected void btnCalculate_Click(object sender, EventArgs e)
  13.         {
  14.             if (IsValid)
  15.             {
  16.                 int monthlyInvestment = Convert.ToInt32(ddlMonthlyInvestment.SelectedValue);
  17.                 decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text);
  18.                 int years = Convert.ToInt32(txtYears.Text);
  19. *
  20.                 decimal futureValue = this.CalculateFutureValue(monthlyInvestment,
  21.                     yearlyInterestRate, years);
  22. *
  23.                 lblFutureValue.Text = futureValue.ToString("c");
  24.             }
  25.         }
  26. *
  27.         protected decimal CalculateFutureValue(int monthlyInvestment,
  28.         decimal yearlyInterestRate, int years)
  29.         {
  30.             int months = years * 12;
  31.             decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;
  32.             decimal futureValue = 0;
  33.             for (int i = 0; i < months; i++)
  34.             {
  35.                 futureValue = (futureValue + monthlyInvestment)
  36.                     * (1 + monthlyInterestRate);
  37.                 if (Trace.IsEnabled)
  38.                 {
  39.                     Trace.Write({i} {futureValue});
  40.                 }
  41.             }
  42.             return futureValue;
  43.         }
  44. *
  45.         protected void btnClear_Click(object sender, EventArgs e)
  46.         {
  47.             ddlMonthlyInvestment.SelectedIndex = 0;
  48.             txtInterestRate.Text = "";
  49.             txtYears.Text = "";
  50.             lblFutureValue.Text = "";
  51.         }
  52.     }
  53. }
Sep 21 '17 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.