Sign in to follow this  
-Gray Fox-

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

Recommended Posts

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
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

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