c# - 从其他类/范围获取 var

标签 c# visual-studio

我才开始使用 C#,但遇到了瓶颈。
我有以下内容:

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 = ;
}

如何获取输出(来自public void button1_Click)并在label1中使用它?

最佳答案

如果这些方法都在同一个类中,使用一个成员变量:

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

关于c# - 从其他类/范围获取 var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39241866/

相关文章:

c# - 获得文件或文件夹的所有权

c# - 确保进行调用以结束方法链

c# - Visual Studio 2015远程调试器 'not implemented'

c# - 代码分析规则 CA1040 : Avoid empty interfaces, 在非空接口(interface)上报错

visual-studio - 在 Visual C# 2010 Express 中集成 Nemerle

c# - 在自定义操作过滤器中重定向

c# - 如何动态增加 C# 中固定数组的大小?

c# - 尝试运行项目 : Access is denied 时出错

c# - 有没有一种聪明的方法来处理 NuGet 中的包依赖关系?

c# - 将本地数据库放在 Visual Studio 解决方案中的什么位置?