如何在事件处理程序中更改value参数? [英] How do I change a value argument from within an event handler?

查看:64
本文介绍了如何在事件处理程序中更改value参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过引用将bool传递给另一个类中的方法,以便可以在方法内部进行更改(-原始参数).

I’m passing a bool to a method in another class by reference, so that I can change it (-the original argument) from within the method.

我还希望一个事件(通过该方法订阅的事件)能够更改它.

I also want an event (which is subscribed to by that method) to be able to change it.

这样做:

class myCheckBox : CheckBox
{
    bool b1;
    public myCheckBox(ref bool b)
    {
        b1 = b;
        this.CheckedChanged += new EventHandler(myCheckBox_CheckedChanged);
    }

    void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        b1 = Checked;
    }
}

没有帮助,因为b1只是b的副本.

doesn’t help, since b1 is only a copy of b.

有什么办法:ref b1 = ref b;吗?如果没有,我该如何解决?

Is there any way of doing: ref b1 = ref b; ? if not, how do I solve this?

(这些示例仅用于解释问题.)

(The examples are only to explain the question.)

推荐答案

传递包含该字段的类,一个表示该字段的字符串,然后保存它们而不是b1.然后使用反射.

Pass the class containing the field, a string denoting it, and save them instead of b1. Then use reflection.

请参见如何创建对以下内容的引用一个值域

这篇关于如何在事件处理程序中更改value参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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