委托方法应在一定条件下自动调用 [英] Delegate method should be invoke automatically by some condition

查看:81
本文介绍了委托方法应在一定条件下自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


       public  delegate void TimeDurationPopup();
       public static int i = 0;
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               TimeDurationPopup f = new TimeDurationPopup(Check);
               f();
           }
       }

       protected void btn_Click(object sender, EventArgs e)
       {
          i += 1;
       }

private void Chec()
{
    if(i==5)
    {
        //TODO my logic
    }
}


在这里,当我单击按钮时,我将增加I值.

如果我重视得到5,我想做一些逻辑.但是我不想像方法那样调用.

我值5时应自动调用该方法.希望我可以通过使用委托来解决此问题.

所以请给我一些想法.

谢谢.


Here when I am clicking the button I am increment the I value.

If I value getting 5 I want to do some logic. But I don''t want call like a method.

That method should be invoke automatically when I value getting 5. I hope I can do this problem by using delegates.

So please give me some idea.

Thanks.

推荐答案

我认为我们不能为整数等数据类型提供委托或事件处理程序.相反,您可以具有这样的功能.

I think we cannot have a delegate or event handler for datatypes like integer. instead you can have a function like this..

Private  int i = 0;

      Public int SetGetValue
      {
         get
           {
                return i;
           }
         set
           {
               if (value == 5)
               {
                   // do your logic
               }
               else
               {
                   this.i=value;
               }
           }
      }

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {

          }
      }

      protected void btn_Click(object sender, EventArgs e)
      {
         this.SetGetValue=(this.SetGetValue)+1;
      }



希望对您有帮助....



hope this helps....


这篇关于委托方法应在一定条件下自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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