You're here: Snippet Directory » Microsoft .NET » C# (33)
Language:

Event handling pattern

Language: English
Programming Language: C#
Published by: Cyron
Last Update: 7/23/2006
Views: 696


Description

A complete eventhandling routine which you can tailor at your desires.

Code

1 namespace Mynamespace 2 { 3 public delegate void DelegateObject(object sender, MyEventArgs e); 4 5 public class MyEventArgs : EventArgs 6 { 7 // See the type "object" just as a template. You can use whatever type you need. 8 public readonly object Variable; 9 10 // See object just as a template. You can use whatever type you need. 11 public MyEventArgs(object Var) 12 { 13 Variable = Var; 14 } 15 } 16 17 public class EventListener 18 { 19 public void ProcessEvent(object sender, MyEventArgs e) 20 { 21 /* Do something with e to process the event. */ 22 } 23 } 24 25 public class Business 26 { 27 public static event DelegateObject EventObject; 28 29 public static void MainMethod() 30 { 31 EventListener eL = new EventListener(); 32 EventObject += new DelegateObject(eL.ProcessEvent); 33 EventFiringMethod(); 34 } 35 36 public static void OnEventFired(MyEventArgs e) 37 { 38 if(EventObject != null) 39 EventObject(new object(), e); 40 } 41 42 public static void EventFiringMethod() 43 { 44 // Just an example 45 /* 46 for(int i = 0; i < 99; i++) 47 { 48 if(i % 7 == 0) 49 { 50 MyEventArgs e1 = new MyEventArgs(i); 51 OnEventFired(e1); 52 } 53 } 54 */ 55 } 56 } 57 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS