当前位置:网站首页>Custom event of C (31)

Custom event of C (31)

2022-07-06 03:47:00 camellias_

Custom events : Be sure to pay attention to the following six parts

1: Declare a delegate about the event

2: Claim event

3: Write the function that triggers the event

4: Create an event handler

5: Register event handlers

6: Triggering event

Defining events :

class dog
    {
        // 1  Declaration delegation 
        public delegate void dogEvent(object sender,EventArgs e);
       
        // 2  Claim event 
        public event dogEvent doger;
 
        // 3  Write the method to trigger the event 
        public  void touch()
        {
            if (this.doger != null)
            {
                Console.WriteLine(@" Here comes the thief    The dog barked .");
                //  The event referenced by the current object 
                this.doger(this,new EventArgs());
            }
        }
    }
 
    class host
    {
        // 4  Write event handlers 
        void hostDog(object sender, EventArgs e)
        {
            Console.WriteLine(" master , I caught the thief ");
        }
 
        // 5  Register event handlers 
        public host(dog dos)
        {
            //  Event registration ( Definition ) Must be used   += 
            dos.doger += new dog.dogEvent(hostDog);
        }
   
    }

When declaring an event event It can be omitted ( It's best not to omit )

// 2  Claim event 
public dogEvent doger;

Event succession

event event It's all inherited EventArgs, By inheritance EventArgs You can add parameters to custom events .

public class AlarmEventArgs : EventArgs
    {
        //  Define the number of thieves 
        public int numberOfThieves;
        //  Assign a value to the number of thieves through the constructor 
        public AlarmEventArgs(int numberValue)
        {
            numberOfThieves = numberValue;
        }  
    }
class Program
    {
        static void Main(string[] args)
        {
            dog dosa = new dog();
            host ho = new host(dosa);
            DateTime now = new DateTime(2019,12,31,23,59,50);
            DateTime minignt = new DateTime(2020,1,1,0,0,0);
 
            Console.WriteLine(" Time passes again ");
 
            while (now < minignt)
            {
                Console.WriteLine(" current time "+now);
                //  Sleep for a second 
                System.Threading.Thread.Sleep(1000);
                //  Add one second to the current time 
                now = now.AddSeconds(1);
            }
            //  Assign a value to the number of thieves 
            AlarmEventArgs e = new AlarmEventArgs(3);
            //  Call the trigger event method 
            dosa.touch(e);
 
            Console.ReadKey();
        }
    }

design sketch :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-pieLUA3K-1656122855547)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e4b07d6147c74508a97c3e6a0f8679d9~tplv-k3u1fbpfcp-zoom-1.image “1556633488315290.png”)]

The test uses all the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace @event
{
    public class AlarmEventArgs : EventArgs
    {
        //  Define the number of thieves 
        public int numberOfThieves;
        //  Assign a value to the number of thieves through the constructor 
        public AlarmEventArgs(int numberValue)
        {
            numberOfThieves = numberValue;
        }  
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            dog dosa = new dog();
            host ho = new host(dosa);
            DateTime now = new DateTime(2019,12,31,23,59,50);
            DateTime minignt = new DateTime(2020,1,1,0,0,0);
 
            Console.WriteLine(" Time passes again ");
 
            while (now < minignt)
            {
                Console.WriteLine(" current time "+now);
                //  Sleep for a second 
                System.Threading.Thread.Sleep(1000);
                //  Add one second to the current time 
                now = now.AddSeconds(1);
            }
            //  Assign a value to the number of thieves 
            AlarmEventArgs e = new AlarmEventArgs(3);
            //  Call the trigger event method 
            dosa.touch(e);
 
            Console.ReadKey();
        }
    }
 
    class dog
    {
        // 1  Declaration delegation 
        public delegate void dogEvent(object sender,AlarmEventArgs e);
       
        // 2  Claim event 
        public event dogEvent doger;
 
        // 3  Write the method to trigger the event 
        public  void touch(AlarmEventArgs e)
        {
            if (this.doger != null)
            {
                Console.WriteLine(@" Here comes the thief    The dog barked .");
                //  The event referenced by the current object 
                //this.doger(this,new EventArgs());
                this.doger(this, e);
            }
        }
    }
 
    class host
    {
        // 4  Write event handlers 
        void hostDog(object sender, AlarmEventArgs e)
        {
            if(e.numberOfThieves <= 1)
            {
                Console.WriteLine(" master , I caught the thief ");
            }
            else
            {
                Console.WriteLine(" master , Too many people , Call the police ");
            }
        }
 
        // 5  Register event handlers 
        public host(dog dos)
        {
            //  Event registration ( Definition ) Must be used   += 
            dos.doger += new dog.dogEvent(hostDog);
        }
   
    }
}

There are good suggestions , Please enter your comments below .

Welcome to personal blog
https://guanchao.site

Welcome to the applet :

 Insert picture description here

原网站

版权声明
本文为[camellias_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060338511775.html