如何在 WinForms 应用程序中的成员之间共享变量(使用 static 关键字)? [英] How do I share variables between members in a WinForms application (use static keyword)?

查看:21
本文介绍了如何在 WinForms 应用程序中的成员之间共享变量(使用 static 关键字)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 WinForms 应用程序内的单击按钮"函数之间共享变量?我尝试阅读它,但我对此很陌生,并没有真正弄清楚 - 据我所知,我必须使用 static 关键字?我真的不知道如何解释它,所以我只会展示一些简单的代码(其中有错误,因为我不知道如何使其工作):

How would I share variables between "Click on a button" functions inside a WinForms application? I tried reading about it, but I'm quite new to this and didn't really get it clear - from what I understand, I have to use the static keyword? I don't really know how to explain it, so I'll just show some simple code (that has mistakes in it, since I don't know how to make it work):

namespace stackoverflow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Not clicked!";
            int number = 0;
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Clicked button 1.";
            number = 1;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = "Clicked button 2.";
            number = 2;
        }
    }
}

我如何让整个班级的数字实际更新......?

How would I make the number actually update in the whole class...?

推荐答案

当您了解无障碍类型的含义时,它会有所帮助.

It helps when you understand what the accessibility types mean.

当某些东西被声明为静态时,这意味着该变量在应用程序的整个生命周期内在内存中占据一个位置.调用或分配给该变量只会影响单个内存位置/偏移量.

When something is declared as static, it means that the variable occupies a single place in memory for the lifetime of the application. Calling or assigning to that variable will only ever affect a single memory location/offset.

非静态变量,如您所说,是类型实例的成员".访问方式的不同之处在于,您需要在变量前面加上包含该变量的类的实例.

Non-Static variables are, as you say, 'members' of an instance of a type. The difference in how they are accessed is that you would need to prefix the variable with the instance of the class which contains the variable.

theFormInstance.Number = 5;

或this.Number=5;

or this.Number=5;

然而,静态变量是通过使用类型名称来引用的.我喜欢将其视为归属"静态变量属于类型",其中非静态变量属于类型的实例"

Static variables however, are referenced by using the type name. I like to think of it as 'belonging' A static variable 'belongs to the type' where a non-static 'belongs to an instance of the type'

Form1.Number= 5;

虽然是的,您可以使用静态变量,但在此特定示例中您不需要,因为您尝试更改的变量与单击事件位于同一个 Form 类中.

While yes, you can use Static variables, you shouldn't need to in this particular example because the variable you are attempting to alter resides within the same Form class as the click events.

只要变量是在点击事件处理程序/方法之外声明的,但在封闭类中 - 那么 Form1 类中的所有方法都可以访问该变量.

As long as the variable is declared outside of the click event handlers/methods, but within the enclosing class - then all methods within the Form1 class will have access to the variable.

注意 - 我怀疑因为您点击了按钮,所以您认为这些按钮对表单类不可见,因为它们是其他控件等.需要记住的是,当按钮触发事件处理程序单击"执行时,它仍然是运行代码的表单.例如:

Note - I suspect that because you were clicking on buttons, you assumed that the buttons wouldn't have visibility to the form class, because they are other controls etc. The thing to remember is that while the buttons are triggering the event handler 'click' to execute, it is still the Form that is running the code. Eg:

MessageBox.Show(this.Name); 

这将返回表单的名称,无论您在表单中的何处执行它,无论是在按钮单击事件中还是在表单加载事件中.

This will return the Form's name, no matter where you execute it within the Form, whether it be in a buttons click event or in a form load event.

如果您想从事件处理程序内部获取按钮,则可以使用sender"变量来引用按钮.(不过,您需要将其转换为正确的类型.

If you wanted to grab the button from inside an event handler, then you could use the 'sender' variable to reference the button. (You would need to cast it to the proper type though.

例如

private void button2_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        label1.Text = "Clicked "+btn.Name;
        number = 2;
    }

无论如何,知道这在几个方向上是可行的,但希望它提供信息并让您深入了解表单中的可访问性.请记住 - 控件存在于表单中,因此它们可以平等地访问表单中声明的​​所有变量 - 只要它们位于表单内部但在方法主体之外.

Anyway, know this goes off in a few directions, but hope its informative and gives you an insight into accessibility within forms. Just remember - Controls live within the Form, so they have equal access to all variables declared within the form - as long as they are inside the form but outside of method bodies.

这篇关于如何在 WinForms 应用程序中的成员之间共享变量(使用 static 关键字)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆