Event Mechanism in C#
2008-03-31 16:02
344 查看
1.what is Event in C#
simplely speaking,Event in C# is that as one instance does one behaviour, it will receive the message and then react with the message.
2.Event Happening&Re-acting Mechanism
3.how to declare a Event
i.first declare a delegte
ii.secondly declare repective Event
like the following codes:
public delegate void MyNewEvent(|object sender|,|MyEventArgs e|);
public event MyNewEvent OnMyNewEvent;
4.Event Demo project
E.g. Cat& Mouse problem.
A mouse wanna steal something, at the same time, A cat is tracking him. Once the mouse is stealing , it will touch something and make noise. and then, the cat will know the mouse is stealing and receive the message and re-act at once. please create a project to realiaze this schema.
the codes are following:
namespace CatMouse
{
public class mouse
{
public delegate void MouseSteel(string food);
public event MouseSteel OnMouseSteel; //declare an Event
public void MouseIsSteeling(string food)
{
Console.WriteLine("Sender side: I am a mouse , I am steeling {0}\n", food);
if (OnMouseSteel != null)
//if there exists a listener who is listen to this event, OnMouseSteel object will not be null; else it will be null;
OnMouseSteel(food);
}
}
public class cat
{
public cat(mouse tmp_mouse)
{
this.A_mouse = tmp_mouse;
A_mouse.OnMouseSteel += new mouse.MouseSteel(A_mouse_OnMouseSteel);
// once the mouse is stealing, the cat will re-act by method <A_mouse_OnMouseSteel>
}
void A_mouse_OnMouseSteel(string food)
{
Console.WriteLine("Receiver side: Cat ! A mouse is steeling {0}\n", food);
}
private mouse A_mouse;
}
class Program
{
static void Main(string[] args)
{
mouse A_mouse=new mouse();
cat A_cat = new cat(A_mouse);
A_mouse.MouseIsSteeling("bread");
A_mouse.MouseIsSteeling("fish");
A_mouse.MouseIsSteeling("milk");
A_mouse.MouseIsSteeling("butter");
Console.Read();
}
}
}
and if everything works well, we will such window:
congratulations! :)
simplely speaking,Event in C# is that as one instance does one behaviour, it will receive the message and then react with the message.
2.Event Happening&Re-acting Mechanism
3.how to declare a Event
i.first declare a delegte
ii.secondly declare repective Event
like the following codes:
public delegate void MyNewEvent(|object sender|,|MyEventArgs e|);
public event MyNewEvent OnMyNewEvent;
4.Event Demo project
E.g. Cat& Mouse problem.
A mouse wanna steal something, at the same time, A cat is tracking him. Once the mouse is stealing , it will touch something and make noise. and then, the cat will know the mouse is stealing and receive the message and re-act at once. please create a project to realiaze this schema.
the codes are following:
namespace CatMouse
{
public class mouse
{
public delegate void MouseSteel(string food);
public event MouseSteel OnMouseSteel; //declare an Event
public void MouseIsSteeling(string food)
{
Console.WriteLine("Sender side: I am a mouse , I am steeling {0}\n", food);
if (OnMouseSteel != null)
//if there exists a listener who is listen to this event, OnMouseSteel object will not be null; else it will be null;
OnMouseSteel(food);
}
}
public class cat
{
public cat(mouse tmp_mouse)
{
this.A_mouse = tmp_mouse;
A_mouse.OnMouseSteel += new mouse.MouseSteel(A_mouse_OnMouseSteel);
// once the mouse is stealing, the cat will re-act by method <A_mouse_OnMouseSteel>
}
void A_mouse_OnMouseSteel(string food)
{
Console.WriteLine("Receiver side: Cat ! A mouse is steeling {0}\n", food);
}
private mouse A_mouse;
}
class Program
{
static void Main(string[] args)
{
mouse A_mouse=new mouse();
cat A_cat = new cat(A_mouse);
A_mouse.MouseIsSteeling("bread");
A_mouse.MouseIsSteeling("fish");
A_mouse.MouseIsSteeling("milk");
A_mouse.MouseIsSteeling("butter");
Console.Read();
}
}
}
and if everything works well, we will such window:
congratulations! :)
相关文章推荐
- C#中的 .NET 弱事件模式 / The .NET weak event pattern in C#
- The relation between event and delegate in visual c#
- C# Delegates And Events in Depth[翻译加笔记]【Event】【2】
- Events and event handling in C#
- Step by Step: Event handling in C#
- Events and event handling in C#
- Click button show the confirmation popup first then call the onlick event in C#
- Add Html/javascript Event In ASP.NET(C#)
- The .NET weak event pattern in C#
- Dynamic in C# 4.0, Part 3
- 191 You observed the following output for a user session: SQL > SELECT sid, event, seconds _in _wait
- Attached Command for Windows 8 Metro Style in C#
- How to call code behind method in JavaScript in C# and VS2010?
- (C#) A demo on how to auto fill out the "account" value in log in web page in "WebBrowser" control.
- Big Event in HDU+完全背包问题
- WPF Recipes in C# 2008: A Problem-Solution Approach
- Delegates and Events in C# / .NET
- C# Heap(ing) Vs Stack(ing) in .NET: Part I
- hdu 1171 Big Event in HDU 多重背包基础题
- C#使用keybd_Event----键盘虚拟键值编码表
about #define, const
C/C++ Basic-- the differs between [malloc&free] and [new &delete]
C/C++ Basics--function pointer
MSMQ Basics- Transactional Messages Processing
MSMQ&Com+ Service: How to create an Com+ Service in .NetFramework
FAQ about AJAX-part I
FAQ about AJAX-part II
Endpoint--Address
Endpoint--Binding