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

边栏推荐
- Pytoch foundation - (1) initialization of tensors
- [slam] orb-slam3 parsing - track () (3)
- Esbuild & SWC: a new generation of construction tools
- RT thread -- FTP of LwIP (2)
- 2.13 weekly report
- JVM的手术刀式剖析——一文带你窥探JVM的秘密
- Four logs of MySQL server layer
- 3.2 rtthread 串口设备(V2)详解
- Indicator system of KQI and KPI
- Prime protocol announces cross chain interconnection applications on moonbeam
猜你喜欢

Facebook等大厂超十亿用户数据遭泄露,早该关注DID了

给新人工程师组员的建议

An article will give you a comprehensive understanding of the internal and external components of "computer"

1.16 - check code

Flask learning and project practice 9: WTF form verification

Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did

RT thread -- FTP of LwIP (2)

SWC introduction

Microkernel structure understanding

SWC介绍
随机推荐
2.1 rtthread pin device details
JS Vanke banner rotation chart JS special effect
Microkernel structure understanding
Multi project programming minimalist use case
Cf464e the classic problem [shortest path, chairman tree]
Do you know cookies, sessions, tokens?
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
Pytoch foundation - (1) initialization of tensors
The solution of permission denied (750 permissions should be used with caution)
C#(二十九)之C#listBox checkedlistbox imagelist
阿里测试师用UI自动化测试实现元素定位
1、工程新建
如何修改表中的字段约束条件(类型,default, null等)
MySQL reads missing data from a table in a continuous period of time
Failure causes and optimization methods of LTE CSFB
Data analysis Seaborn visualization (for personal use)
Exchange bottles (graph theory + thinking)
Flask learning and project practice 8: introduction and use of cookies and sessions
SWC介绍
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did