当前位置:网站首页>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 :
边栏推荐
- 3.1 detailed explanation of rtthread serial port device (V1)
- 3.1 rtthread 串口设备(V1)详解
- 如何修改表中的字段约束条件(类型,default, null等)
- Flask learning and project practice 8: introduction and use of cookies and sessions
- 遥感图像超分辨率论文推荐
- How to modify field constraints (type, default, null, etc.) in a table
- Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
- Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
- 2.1 rtthread pin设备详解
- Failure causes and optimization methods of LTE CSFB
猜你喜欢
Factors affecting user perception
BUAA计算器(表达式计算-表达式树实现)
C#(二十七)之C#窗体应用
How do we make money in agriculture, rural areas and farmers? 100% for reference
[practice] mathematics in lottery
Facebook等大厂超十亿用户数据遭泄露,早该关注DID了
Alibaba testers use UI automated testing to achieve element positioning
Exchange bottles (graph theory + thinking)
MySQL reads missing data from a table in a continuous period of time
BUAA calculator (expression calculation - expression tree implementation)
随机推荐
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
Shell pass parameters
在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
Flask learning and project practice 9: WTF form verification
P7735-[noi2021] heavy and heavy edges [tree chain dissection, line segment tree]
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
MySQL reads missing data from a table in a continuous period of time
Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
Introduction to data types in MySQL
Python implementation of maddpg - (1) openai maddpg environment configuration
C language circular statement
Codeforces Global Round 19
How to standardize the deployment of automated testing?
Pytoch foundation - (1) initialization of tensors
Exchange bottles (graph theory + thinking)
[practice] mathematics in lottery
Ethernet port &arm & MOS &push-pull open drain &up and down &high and low sides &time domain and frequency domain Fourier
Multi project programming minimalist use case
Quartz misfire missed and compensated execution
JVM的手术刀式剖析——一文带你窥探JVM的秘密