Static Methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr. Bean

    Static Methods

    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.ChildColl ection("log_tb" ).text but it didnt work Any Ideas?

  • Jon Skeet [C# MVP]

    #2
    Re: Static Methods

    Mr. Bean <mkhiami@gmail. com> wrote:[color=blue]
    > 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.ChildColl ection("log_tb" ).text but it didnt work Any Ideas?[/color]

    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?

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Vladimir Matveev

      #3
      Re: Static Methods

      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
      }

      Comment

      • Mr. Bean

        #4
        Re: Static Methods

        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?

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Static Methods

          Mr. Bean wrote:[color=blue]
          > class Form1{
          > public x.x.TextBox log_tb;
          >
          > form1(){
          > //constructor
          > }
          > public static void log(string x){
          > // i cannnt call log_tb.Text from here,
          > }
          > }[/color]

          No, you wouldn't be able to, because it wouldn't know which form's
          log_tb you're trying to use.
          [color=blue]
          > 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?[/color]

          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

          Comment

          Working...