当前位置:网站首页>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 :

边栏推荐
- Introduction to data types in MySQL
- Four logs of MySQL server layer
- MySQL reads missing data from a table in a continuous period of time
- Exchange bottles (graph theory + thinking)
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- 如何修改表中的字段约束条件(类型,default, null等)
- 【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
- Align items and align content in flex layout
- The solution of permission denied (750 permissions should be used with caution)
- Why do you want to start pointer compression?
猜你喜欢

教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )

在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒

Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did

JVM的手术刀式剖析——一文带你窥探JVM的秘密

Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation

登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art

WPF效果第一百九十一篇之框选ListBox

自动化测试怎么规范部署?

MySQL about self growth
随机推荐
Containerization Foundation
Pytorch基础——(2)张量(tensor)的数学运算
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Python implementation of maddpg - (1) openai maddpg environment configuration
BUAA喜鹊筑巢
Esbuild & SWC: a new generation of construction tools
Prime protocol announces cross chain interconnection applications on moonbeam
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
Blue style mall website footer code
C#(二十七)之C#窗体应用
Four logs of MySQL server layer
Yyds dry inventory what is test driven development
Indicator system of KQI and KPI
Mysqldump data backup
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
Map sorts according to the key value (ascending plus descending)
3.1 detailed explanation of rtthread serial port device (V1)
[analysis of variance] single factor analysis and multi factor analysis
2.2 fonctionnement stm32 GPIO
Codeforces Global Round 19