Static Methods

M

Mr. Bean

I building a small win application that has one form and several
classes. I want to allow each class to give a log about its activities,
so I created a static method at the form and called it from each class.

ex Form1.echo("the message");
//in the form
staic void echo(string x){
log_tb.Text += x;
// the static mehtod isn't locating the texxtbox log_tb..
}

the static function echo is suposed to append the text to a textbox,
but I'm not able to accoplish that. I also tried calling the
Form1.ChildCollection("log_tb").text but it didnt work Any Ideas?
 
J

Jon Skeet [C# MVP]

Mr. Bean said:
I building a small win application that has one form and several
classes. I want to allow each class to give a log about its activities,
so I created a static method at the form and called it from each class.

ex Form1.echo("the message");
//in the form
staic void echo(string x){
log_tb.Text += x;
// the static mehtod isn't locating the texxtbox log_tb..
}

the static function echo is suposed to append the text to a textbox,
but I'm not able to accoplish that. I also tried calling the
Form1.ChildCollection("log_tb").text but it didnt work Any Ideas?

Well, you haven't shown the code for Form1. Is log_tb an instance
variable? If so, *which* form do you want it to change? What if no
instances of Form1 have been created yet?
 
V

Vladimir Matveev

If log_tb isn't static class field it shouldn't be accessible from
class static methods, because it requires "this" reference, that is
absent in static members

Possble solution : pass reference to form to static method
ex:
public static void echo(string text, Form form)
{
form.log_tb += text
}
 
M

Mr. Bean

class Form1{
public x.x.TextBox log_tb;

form1(){
//constructor
}
public static void log(string x){
// i cannnt call log_tb.Text from here,
}
}
----------

I read other posts about similar topics, and I used this workaround :to
use a streamwriter in the static method log(sring x) and a an event
which reads the text text from the text file.
usage : form1.log("this text wiull be added to the text file");
in form1 I have an event handler which will load the text from the text
file into a local instance of a text box.

However, was there any better way for me to do this?
 
J

Jon Skeet [C# MVP]

Mr. Bean said:
class Form1{
public x.x.TextBox log_tb;

form1(){
//constructor
}
public static void log(string x){
// i cannnt call log_tb.Text from here,
}
}

No, you wouldn't be able to, because it wouldn't know which form's
log_tb you're trying to use.
I read other posts about similar topics, and I used this workaround :to
use a streamwriter in the static method log(sring x) and a an event
which reads the text text from the text file.
usage : form1.log("this text wiull be added to the text file");
in form1 I have an event handler which will load the text from the text
file into a local instance of a text box.

However, was there any better way for me to do this?

Well, to be honest I think the best thing to do now would be to study
the basics of C#, particularly in terms of static members and instance
members, and what the difference between them is. It's really important
that you understand this.

For this particular issue, it would be reasonable to have a private
static event which is raised in the Log method and which individual
form instances could subscribe to (and unsubscribe from in their
Dispose method).

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top