Hi Patrick,
I've copied and pasted some code below showing how I was trying to do it on
my first try. I also tried putting it in the constructor of the form (see
"current try" below) and this was an imporovement. This way I'm not getting
any errors in the constructor. I'm now getting errors in using it.
I'm using the WriteLine method in my catch statements of various methods in
my frm2085.cs and will be using it in a few other classes. So in my form, I
have a catch statement that looks like this:
catch (Exception e)
{
TextBoxTracer.WriteLine(e.Message);
}
and I get this error when it builds:
"Error 3 The name 'TextBoxTracer' does not exist in the current context"
Should the lines I now have in my constructor be cut and pasted into each of
my catch statements or is there something else I should try?
///////////////////////////////////////////////////////
// first try
////////////////////////////////////////////////////
public partial class frm2085 : Form
{
private Excel.ApplicationClass _excel;
private Excel.Workbook _book;
protected Excel.Sheets _sheets;
protected Excel.Worksheet _sheet;
private Int32 headerRow;
TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog);
Trace.Listeners.Add(TextBoxTracer);
public frm2085()
{
InitializeComponent();
// create a new COM object for excel
_excel = new ApplicationClass();
}
///////////////////////////////////////////////////////
// current try
////////////////////////////////////////////////////
public frm2085()
{
InitializeComponent();
// create a new COM object for excel
_excel = new ApplicationClass();
TextboxTraceListener TextBoxTracer = new
TextboxTraceListener(txtLog);
Trace.Listeners.Add(TextBoxTracer);
}
"Patrick Steele" wrote:
In article <47**********************************@microsoft.co m>,
ar*********@discussions.microsoft.com says...
I want to send trace messages to a textbox in my winforms application using
C# 2.0. I have a custom TraceListener class (see below) that is very basic,
but I'v got an error implementing it that I don't understand. To instantiate
my class I have:
TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog);
Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm2085.txtLog"
Where is this code executing? Where in your app is the line of code
shown above?
--
Patrick Steele
http://weblogs.asp.net/psteele