当变量的值改变如何触发事件? [英] How to trigger event when a variable's value is changed?

查看:172
本文介绍了当变量的值改变如何触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建使用Visual Studio C#中的应用程序。我想,这样当一个变量具有1的值,那么某一段code是开展了创建一些code。
我知道我可以使用if语句,但问题是该值将在一个异步过程,以便在技术上要改变的,如果该值已改变之前的语句可以忽略不计。

I'm currently creating an application in C# using Visual Studio. I want to create some code so that when a variable has a value of 1 then a certain piece of code is carried out. I know that I can use an if statement but the problem is that the value will be changed in an asynchronous process so technically the if statement could be ignored before the value has changed.

是否有可能,这样,当变量值变化时触发事件创建一个事件处理程序?如果是这样,我怎么能这样做呢?

Is it possible to create an event handler so that when the variable value changes an event is triggered? If so, how can I do this?

这是完全可能的,我可能误解了如何if语句的作品!任何帮助将是非常美联社preciated。

It is completely possible that I could have misunderstood how an if statement works! Any help would be much appreciated.

推荐答案

在我看来。

public int MyProperty
{
    get { return _myProperty; }
    set
    {
        _myProperty = value;
        if (_myProperty == 1)
        {
            // DO SOMETHING HERE
        }
    }
}

这允许你运行一些code随时属性值的变化。你可以在这里引发事件,如果你想要的。

This allows you to run some code any time the property value changes. You could raise an event here, if you wanted.

这篇关于当变量的值改变如何触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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