zoukankan      html  css  js  c++  java
  • 事件和委托

    1.定义
    public class Class1
    {
    public string bb = string.Empty;
    public Class1()
    {
    //
    // TODO: Add constructor logic here
    //

    }

    //定义一个无返回值的委托(也可以定义有返回值的)
    public delegate void mydelegate(string obj);
    //委托、方法、事件返回值和参数类型要一致。
    public void getStr(string x)
    {
    bb = x;
    }
    //属于委托mydelegate类型的事件类型
    public event mydelegate myevent;
    //启动事件的方法
    public void execMyevent(string x)
    {
    myevent(x);
    ////同步
    //myevent.Invoke(x);
    ////异步
    //myevent.BeginInvoke(x);
    }
    }

    2.使用
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Class1 cs1 = new Class1();
    //事件挂载属于事件委托类型mydelegate的委托mydelegate类型
    cs1.myevent += new Class1.mydelegate(cs1.getStr); //+=(-=)Lambda表达式
    //调用启动事件的方法
    cs1.execMyevent("被调用");

    Label1.Text = cs1.bb;
    }

    }

  • 相关阅读:
    Swap Nodes in Pairs
    Permutations(copy)
    Sort Colors
    Merge Two Sorted Lists
    Implement Queue using Stacks
    Best Time to Buy and Sell Stock
    Happy Number
    Gray Code
    springMVC初次搭建,产生错误
    JSP常用指令
  • 原文地址:https://www.cnblogs.com/it-xcn/p/5889299.html
Copyright © 2011-2022 走看看