Explaining Delegates in C# - Part 3 (Events 2)
I was thinking that the previous post on Events and Delegates was quite self-explanatory. A couple of days ago, I received an email which said that may be it is not that good as to explain what I want to achieve in life through that event. So, I thought why not write another post on Delegates and Events to make things clearer from a practical (or may be not so practical, "BUT" just another simple and practical example. There is no harm in trying, after all...)
Here is the requirement...
You have a class with a special number. You have another (or may be a lot of other) class which is using it. The requirement now is that, whenever the special number is changed, all the other guys should be notified about what this number was and what it has become!
In this case,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace EventAndDelegateDemo
{
//There are 3 rules which are MANDATORY when you are planning to create events
//Rule 1> The delegate must be defined with two arguments
//Rule 2> These arguments always represent TWO objects…
// The Publisher (object that raised the event)
// Event Information object
//Rule 3> The 2nd object HAS to be derived from EventArgs //Step 1> Create a class that inherits from EventArgs (To comply with Rule 3)
class NotifyChangeEventArgs : EventArgs
{
//constructor to initialize the SpecialNumberOld and SpecialNumberNew property
public NotifyChangeEventArgs(int SpecialNumberOld, int SpecialNumberNew)
{
this.SpecialNumberNew = SpecialNumberNew;
this.SpecialNumberOld = SpecialNumberOld;
}
public int SpecialNumberNew { get; set; }
public int SpecialNumberOld { get; set; }
}; class SpecialNumberClass // Publisher
{
//Publisher declares a special delegate and event (Rule 1 & 2)
public delegate void SpecialNumberHandler(object source, NotifyChangeEventArgs e);
public event SpecialNumberHandler OnSpecialNumberUpdate; //Constructor to set the value of the _specialNumber directly.
//Notice that we are not setting the SpecialNumber property!
public SpecialNumberClass(int number)
{
_specialNumber = number;
} //This property will fire an event called OnSpecialNumberUpdate whenever called
//The effect is that is the special number is changed from outside, all the guys
//would be notified. It is upto them to listen to it or not listen to it.
private int _specialNumber;
public int SpecialNumber
{
get { return _specialNumber; }
//Put a breakpoint here on set and step into (F11)
set
{
if (OnSpecialNumberUpdate != null)
{
//This is the guy who would fire that notify event called OnSpecialNumberUpdate
//Basically, before you fire that event, you can set up that value for EventArgs
//In my case, I have set the value of e's old value and new value...
//to the _specialNumber and value (which would be the new value)
//Notice that we are just firing the events with appropriate EventArgs...
//We haven't thrown any output as such yet!!!!
NotifyChangeEventArgs e = new NotifyChangeEventArgs(_specialNumber, value);
Console.WriteLine("Raising Events to all the subscribers...\n");
OnSpecialNumberUpdate(this, e);
}
}
}
}; class SpecialNumberUpdateListener // Subscriber
{
//Here we are just creating a new Object called objSN for my SpecialNumberClass
SpecialNumberClass objSN; //In this method, I would go ahead and bind my event
public SpecialNumberUpdateListener(SpecialNumberClass spClass)
{
Console.WriteLine("SpecialNumber listener > Subscribing to the event...\n");
this.objSN = spClass; //Wiring the event so that the event is fired
spClass.OnSpecialNumberUpdate += new SpecialNumberClass.SpecialNumberHandler(OnSpecialNumberUpdate);
} //This is the event that would be invoked whenever you change the value
//Try putting a break point here and see how it gets hit when the number changes
//Also notice how we use the Event args to grab the details and finally print it out
void OnSpecialNumberUpdate(object source, NotifyChangeEventArgs e)
{
Console.WriteLine("The number has changed from {0} to {1} ", e.SpecialNumberOld, e.SpecialNumberNew);
}
} class EventDemo
{
public static void Main()
{
//Creating a new Special Number (Publisher) class with initial value of 20
SpecialNumberClass snc = new SpecialNumberClass();
//Creating a Subscriber/listener class
SpecialNumberUpdateListener s = new SpecialNumberUpdateListener(snc);
//Changing the value so that the event is triggered.
//Put a breakpoint and step into the code (F11)
snc.SpecialNumber = ;
Console.ReadLine();
}
}
}
转:http://www.dotnetscraps.com/dotnetscraps/post/Explaining-Delegates-in-C-Part-3-(Events-again).aspx
Explaining Delegates in C# - Part 3 (Events 2)的更多相关文章
- Explaining Delegates in C# - Part 2 (Events 1)
In my previous post, I spoke about a few very basic and simple reasons of using delegates - primaril ...
- Explaining Delegates in C# - Part 1 (Callback and Multicast delegates)
I hear a lot of confusion around Delegates in C#, and today I am going to give it shot of explaining ...
- Explaining Delegates in C# - Part 6 (Asynchronous Callback - Way 3)
By now, I have shown the following usages of delegates... Callback and Multicast delegatesEventsOne ...
- Explaining Delegates in C# - Part 4 (Asynchronous Callback - Way 1)
So far, I have discussed about Callback, Multicast delegates, Events using delegates, and yet anothe ...
- Explaining Delegates in C# - Part 7 (Asynchronous Callback - Way 4)
This is the final part of the series that started with... Callback and Multicast delegatesOne more E ...
- Explaining Delegates in C# - Part 5 (Asynchronous Callback - Way 2)
In this part of making asynchronous programming with delegates, we will talk about a different way, ...
- 11.Events
1.A type that defines an event member allows the type (or instances of the type) to notify other obj ...
- VB.NET vs. C#
VB.NET Program Structure C# Imports System Namespace Hello Class HelloWorld Overloads Shar ...
- [转]c#.NET和VB.NET语法的比较
本文转自:http://www.cnblogs.com/lify0407/archive/2007/08/01/838589.html c#.NET和VB.NET语法的比较 VB.NET C# C ...
随机推荐
- Sword STL之仿函数概念介绍
--介绍 函数和类似函数的对象(仿函数)遍布STL.关联容器使用它们来使元素保持有序:find_if这样的算法使用它们来控制它们的行为: 如果缺少它们,那么比如for_each和transform这样 ...
- install Mac OS on Vmware
本文将用详尽的图文介绍如何在虚拟机中安装运行Mac OS X,只要严格地按照本教程一步步操作,即使不了解苹果电脑的读者,也能顺利地安装并运行Mac OS X. 一.准备工作 VMWare 12 Pro ...
- USB学习笔记连载(十五):USB固件更新以及安装驱动
前几篇博客已经把如何更改固件程序和更改USB驱动名称,那么接下来就要把之前生成的 .iic 文件烧录到EEPROM里面去,实现USB的C2启动(笔者使用的是此类型,C2启动). 打开Cypress U ...
- 安装GYP(Generate Your Projects)
GYP(Generate Your Projects)是由 Chromium 团队开发的跨平台自动化项目构建工具,Chromium 便是通过 GYP 进行项目构建管理. 主页 :http://code ...
- python numpy的transpose函数用法
#MXNET的N*C*H*W在numpy打印时比较直观#mxnet卷积层# 输入数据格式是:batch * inchannel * height * width# 输出数据格式是:batch * ou ...
- c 变量的存储类型auto等(基础知识)和c函数变量
总结 1).在c语言中每一个变量和函数有两个属性:数据类型和数据的存储类别. 2). 对数据型(如整型.字符型等).存储类别指的是数据在内存中存储的方式. 存储方式分为两大类: 静态存储类和动态存储类 ...
- e812. 强制弹出菜单为重组件
By default, Swing popup menus used by JMenu and JPopupMenu are lightweight. If heavyweight component ...
- (转)关于yuv 格式:planar和semi-planar格式
关于yuv 格式 YUV 格式通常有两大类:打包(packed)格式和平面(planar)格式.前者将 YUV 分量存放在同一个数组中,通常是几个相邻的像素组成一个宏像素(macro-pixel);而 ...
- SOFA企业应用框架
前言 从业这么多年,接触过银行的应用,Apple的应用,eBay的应用和现在阿里的应用,虽然分属于不同的公司,使用了不同的架构,但有一个共同点就是都很复杂.导致复杂性的原因有很多,如果从架构的层面看, ...
- Windows7双系统卸载Ubuntu
正确的删除ubuntu方法如下: 第1步,修复MBR 1.进入win7,下载个软件MbrFix.exe,放在C:\windows\system32文件夹中 2.点击开始>所有程序>附件&g ...