Sign in to follow this  
-Gray Fox-

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

Recommended Posts

-Gray Fox-    122
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!

Share this post


Link to post
Share on other sites
SiCrane    11839
Which textBox1 are you expecting to change? One on Form1 whose button you pushed or one on the Post object that you just created?

Share this post


Link to post
Share on other sites
-Gray Fox-    122
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 :)

Share this post


Link to post
Share on other sites
Kaze    948
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;
}
}

}


Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this