posts - 77, comments - 54, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

关于事件和委托的理解2

Posted on 2006-05-31 12:23 东人EP 阅读(153) 评论(0)  编辑 收藏 引用 所属分类: .NET
事件和委托联合实例:
 1using System;
 2
 3namespace EventHandler
 4{
 5    delegate void DlgDoCalcDelegate();
 6    /// <summary>
 7    /// Class1 的摘要说明。
 8    /// </summary>

 9    class EventsHandler
10    {
11        private void DoAdd()
12        {
13            AddMethod();
14        }

15
16        private void AddMethod()
17        {
18            long lngFirstValue;
19            long lngSecondValue;
20            long lngSum;
21            Console.WriteLine("请输入第一个进行运算的数值:");
22            lngFirstValue = long.Parse(Console.ReadLine());
23            Console.WriteLine("请输入第二个进行运算的数值:");
24            lngSecondValue = long.Parse(Console.ReadLine());
25            lngSum = lngFirstValue + lngSecondValue;
26            Console.WriteLine("{0}与{1}的相加等于{2}", lngFirstValue, lngSecondValue, lngSum);
27        }

28
29        /// <summary>
30        /// 应用程序的主入口点。
31        /// </summary>

32        [STAThread]
33        static void Main(string[] args)
34        {
35            EventsHandler myEventHandler = new EventsHandler();
36            calc mycalc = new calc();
37            mycalc.evtDoCalc += new DlgDoCalcDelegate(myEventHandler.DoAdd);
38            Console.WriteLine("触发加法运算事件------");
39            mycalc.DoCalc();
40            Console.Read();
41        }

42
43    }

44
45    class calc
46    {
47        public event DlgDoCalcDelegate evtDoCalc;
48        public void DoCalc()
49        {
50            evtDoCalc();
51        }

52    }

53}

54
只有注册用户登录后才能发表评论。