Deligate, Multicast deligate in C#
| A delegate is a class that can contain a reference to an event handler function that matches the delegate signature. Delegate can be of anytype,
Each delegate contains a reference to an event handler, and each event handler executed.
The sender of the event does not know which part of the application will handle the event, or even if it will be handled. |
 | Create Deligate |
| using System;
| | 1:
| | 2:namespace Arindam.CSharp
| | 3:{
| | 4:
| | 5: public enum Status {High,Medium,Low}
| | 6:
| | 7: /*
| | 8: Now create the delegate. this delegate will have a return void type and take ETGCarEvetArgs as argument.
| | 9: * The delegate does not have to be declared inside a class. All our event handlers will have the same signature as this delegate.
| | 10:
| | 11: But Delegate can be of any type, void/int/string/bool/or any custom type
| | 12: *
| | 13: * Also we can create a generic delegate [shown in third example]
| | 14: */
| | 15:
| | 16:
| | 17: // Delegate 1. custom return type
| | 18: public delegate Status ETGStatusEventhandler(ETGCarEvetArgs args);
| | 19:
| | 20: // Delegate 2. custom return type
| | 21: public delegate void ETGEventhandler(object sender, ETGCarEvetArgs args);
| | 22:
| | 23: // Delegate 3. this is how we use generic to create a delegate
| | 24: public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
| | 25:
| | 26:
| | 27:
| | 28:
| | 29:
| | 30:}
| | 31: |
|
|
| Create custom event argument, in this class we can put any information that may be required to carry out any event. |
 | Custom event argument |
| using System;
| | 1:
| | 2:namespace Arindam.CSharp
| | 3:{
| | 4: public class ETGCarEvetArgs : System.EventArgs
| | 5: {
| | 6: private int _Speed;
| | 7: private string _Message;
| | 8: private string _Color;
| | 9:
| | 10: public int Speed
| | 11: {
| | 12: get { return _Speed; }
| | 13: set { _Speed = value; }
| | 14: }
| | 15:
| | 16: public string Message
| | 17: {
| | 18: get { return _Message; }
| | 19: set { _Message = value; }
| | 20: }
| | 21: public string Color
| | 22: {
| | 23: get { return _Color; }
| | 24: set { _Color = value; }
| | 25: }
| | 26: }
| | 27:}
| | 28: |
|
|
| Here we see the implementation. |
 | Implementation |
|
| | 1:namespace Arindam.CSharp
| | 2:{
| | 3: public class ETGCar
| | 4: {
| | 5: //Create any event handler of delegate type
| | 6: public event ETGEventhandler onCarStart;
| | 7: public event ETGStatusEventhandler onSpeedChange;
| | 8:
| | 9: /* Generic example:
| | 10: public event EventHandler<NameEventArgs> NameReceived;
| | 11: public event EventHandler<WorkflowMessageEventArgs> NameRequested;
| | 12: */
| | 13:
| | 14: public ETGCar()
| | 15: {
| | 16:
| | 17: }
| | 18:
| | 19: public void Start(ETGCarEvetArgs args)
| | 20: {
| | 21:
| | 22: this.onCarStart += new ETGEventhandler(ETGCar_onCarStart);
| | 23: //this.onSpeedChange +=new ETGStatusEventhandler(ETGCar_onSpeedChange);
| | 24:
| | 25: }
| | 26:
| | 27: public void Stop(ETGCarEvetArgs args)
| | 28: {
| | 29: this.onCarStart += new ETGEventhandler(ETGCar_onCarStart);
| | 30: // this.onSpeedChange += new ETGStatusEventhandler(ETGCar_onSpeedChange);
| | 31:
| | 32: }
| | 33:
| | 34:
| | 35: protected void ETGCar_onCarStart(object sender, ETGCarEvetArgs e)
| | 36: {
| | 37: string _Message = "";
| | 38:
| | 39: switch (e.Speed)
| | 40: {
| | 41: case 80:
| | 42: _Message = "High Speed";
| | 43: break;
| | 44: case 60:
| | 45: _Message = "Medium Speed";
| | 46: break;
| | 47: case 40:
| | 48: _Message = "Low Speed";
| | 49: break;
| | 50:
| | 51: }
| | 52: }
| | 53:
| | 54: protected Status ETGCar_onSpeedChange(object sender, ETGCarEvetArgs e)
| | 55: {
| | 56: return Status.High;
| | 57: }
| | 58:
| | 59:
| | 60: }
| | 61:}
| | 62: |
|
|
Asp.net, Ado.net, .Net Remoting, .Net
Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML