如何在 C# 中的方法之间传递变量?

How do I pass variables between methods in C#?

我正在尝试构建一个程序,但我意识到我无法访问某个变量,因为它是用另一种方法创建的。

如何将变量传递给另一个方法?

这是我尝试做的一个例子:

namespace Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            // Get the "text" variable and use it
            label1.Text = text;
        }
    }
}

你快到了。为了共享状态,在 class 中拥有私有变量是面向对象编程中的常见做法。在您的 class 中添加一个变量。它将在所有方法中可用,并可用于在它们之间共享数据(这是许多方法中的一种):

namespace Example
{
    public partial class Form1 : Form
    {
        private string inputText { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            inputText = textBox1.Text;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            // Get the "text" variable and use it
            label1.Text = inputText;
        }
    }
}

您的示例有一个名为 Form1Form,它有一个名为 button1Button,一个 TextBox 命名为 textbox1 和一个 Label 命名为 label1.

您尝试的场景是:

  1. 用户在 textbox1
  2. 中输入了一些文本
  3. 用户点击 button1,这将保存来自 textbox1
  4. 的当前值
  5. 用户点击 label1,这将显示在上一步中存储的值

请务必了解,在这种情况下,我们不会尝试在两种方法之间传递值,因为按钮点击和标签点击可以彼此独立发生,所以这真的就像计算器上的记忆存储 (M+) 和记忆调用 (MR) 按钮。 要实现此 storage,您应该在Form1 class,Form1 class.

的同一实例上的其他方法可以访问此方法

See Working with Instance and Local variables for a practical explanation

  1. 创建一个字段属性store 值,对于您的特定示例,两者都可以,但是为了熟悉 C# 技术,我建议您从 属性 开始,因为它更好地封装了 存储值以供以后使用的场景使用 及更高版本可能会增加实际存储值的方式和位置。

    See What is the difference between a field and a property? for a healthy discussion
    Until you need to change the implementation, you can simply use an Auto-Property

     public string StoredText { get; set; }
    
  2. 现在,在 button1click 事件处理程序中,我们可以 设置 StoredText 属性 在 Form1 实例上

     private void button1_Click(object sender, EventArgs e)
     {
         this.StoredText = textBox1.Text;
     }   
    

    set is a term we use for saving a value into a property in c# Note the use of the this keyword, it is optional in this case, or can be inferred by the compliler, it indicates that we want to reference a member on the instance of the class, not a variable that might have the same name within the same method scope of the line of code that is executing.

  3. 最后,在 label1click 事件处理程序中,我们可以 get 之前的值存储在 Form1 实例中的 StoredText 属性 中。

     private void label1_Click(object sender, EventArgs e)
     {
         // Get the "StoredText" variable and use it
         label1.Text = this.StoredText;
     }
    

    get is a term we use for accessing a value from a property in c# this is not required, but can be helpful to understand that we are accessing a member that is outside of the current method scope.

加在一起看起来像这样:

namespace Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>Saved value from see <see href="textBox1"/></summary>
        public string StoredText { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            this.StoredText = textBox1.Text;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            // Get the "StoredText" variable and use it
            label1.Text = this.StoredText;
        }
    }
}

您可能没有注意到 textBox1label1 本身实际上是 instance variables,它们在 InitializeComponent() 执行时在单独的代码文件中初始化构造函数。 出于这个原因,您根本 不需要 存储该值,您可以简单地重写 button1 的客户端事件处理程序以直接写入 label

label1.Text = textBox1.Text;

可以在没有中间存储的情况下直接在方法之间传递变量,这是另一天的课程,将涉及 return 语句 and/or parameters 您的方法。 然而,在这种情况下,无法使用这些方法的 return 或其他参数,因为这些 事件处理程序 需要特定的方法签名才能按预期运行。