Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by -Gray Fox- Aug 20, 2008 at 5:35 PM 4 replies 850+ views
Original Post
-Gray Fox-
-Gray Fox-

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!
SiCrane
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?
-Gray Fox-
-Gray Fox-
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 :)

Kaze
Kaze
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;        }    }}
SiCrane
SiCrane
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.
-Gray Fox-
-Gray Fox-
Thanks lol... it was so easy lol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.