Home   Cover Cover Cover Cover
 

Debug output

Question: see book

The source code given for this exercise contains a bug:
the constructor must, of course, have the same name as the class, thus changing

Test ()       to       Adder ()

The same applies to the object creation in the Main method:

new Test()       ->       new Adder ()

Answer: see 8.4.3 System.Diagnostics.Debug

The modified source code can look like this:

AdderExt.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

class Adder : Form {
  TextBox amount = new TextBox();
  Button b = new Button();
  Label total = new Label();
  
  Adder () {
    amount.Left = 10; amount.Top = 10;
    Controls.Add(amount);
    
    b.Text = "ADD";
    b.Left = 10; b.Top = 40;
    b.Click += new EventHandler(Add);
    Controls.Add(b);
    
    total.Text = "0";
    total.Left = 10; total.Top = 70;
    Controls.Add(total);
  }

  private void Add (object sender, EventArgs eargs) {
    Debug.WriteLine(amount.Text);
    total.Text = (Convert.ToInt32(amount.Text) +
                  Convert.ToInt32(total.Text)).ToString();
  }
  
  public static void Main () {
    Debug.Listeners.Add(new TextWriterTraceListener(File.AppendText("amount.log")));
    Debug.WriteLine(DateTime.Now);
    Debug.Indent();
    Application.Run(new Adder());
    Debug.Unindent();
    Debug.Close();
  }
}