[.net] Change the Text value by a function in a class.

Started by
4 comments, last by -Gray Fox- 15 years, 8 months ago

namespace Socket
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Post post = new Post();
            post.write();
        }
        

    }

    public class Post : Form1
    {
        public void write()
        {
           [...]
           log(textbox1, "Shutting Down");
        }
        public void log(TextBox target, string output)
        {
            target.Text = output;
        }
    }

}
Let's say that textbox1 is a textbox and button1 a button, why this piece of code doesn't work? the textbox remains blank!
Advertisement
Which textBox1 are you expecting to change? One on Form1 whose button you pushed or one on the Post object that you just created?
Quote:Original post by SiCrane
Which textBox1 are you expecting to change? One on Form1 whose button you pushed or one on the Post object that you just created?


The one on Form1 :)

a object that inherits from another will still have its own copy of all non-static variables

try
namespace Socket{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();                  }        private void button1_Click(object sender, EventArgs e)        {            Post post = new Post(textbox1);            post.write();        }    }    public class Post    {        public Post(TextBox textbox){          this.textbox=textbox;       }       TextBox textbox;        public void write()        {           [...]           log(textbox, "Shutting Down");        }        public void log(TextBox target, string output)        {            target.Text = output;        }    }}
But you're passing the one on the new Post object to the log() function. You need to pass the one from the Form1 object.
Thanks lol... it was so easy lol

This topic is closed to new replies.

Advertisement