Lee Ikard
Lee Ikard

Reputation: 76

Get var from other class/scope

I've only started C#, but I've hit a brick wall.
I have the following:

public void button1_Click(object sender, EventArgs e)
{
    // Start the child process.
    Process p = new Process();
    // Redirect the output stream of the child process.
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe";
    p.StartInfo.Arguments = textBox1.Text;
    p.Start();
    // Do not wait for the child process to exit before
    // reading to the end of its redirected stream.
    // p.WaitForExit();
    // Read the output stream first and then wait.
    string output = p.StandardOutput.ReadToEnd();
    MessageBox.Show(output);

    p.WaitForExit();
}

public void label1_Click(object sender, EventArgs e)
{
    label1.Text = ;
}

How can I get output (From public void button1_Click) and use it in label1?

Upvotes: 1

Views: 50

Answers (2)

Lance Hall
Lance Hall

Reputation: 202

You could set label1 from the button click event.

public void button1_Click(object sender, EventArgs e)
    {

        // Start the child process.
        Process p = new Process();
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe";
        p.StartInfo.Arguments = textBox1.Text;
        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        label1.Text = p.StandardOutput.ReadToEnd();
        MessageBox.Show(output);

        p.WaitForExit();
    }

if that is what you are going for

Upvotes: 2

Jacob
Jacob

Reputation: 78840

If these methods are all in the same class, use a member variable:

public class YourObject 
{
    private string _output;

    public void button1_Click(object sender, EventArgs args)
    {
        // ...
        _output = output;
    }

    public void label1_Click(object sender, EventArgs e)
    {
        label1.Text = _output;
    }
}

Upvotes: 3

Related Questions