DHTMLAsp.NetSQLC-SharpXMLFrameworkIISXMLWebServiceArchitecture
UMLProject ManagementSDLCMethodologiesDesign PatterenOOPWCF.Net RemotingWWF
MVPMVCSite Map
Asp.net 2.0
User Control
Custom Control
File Handling
Reflection
Deligate
Site Map
Tell a Friend
You
Friend
Email

Newsletter
Email
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;
namespace ETG.Base.Applications.ETigers.Web.ETech.CSharp
{
public enum Status {High,Medium,Low}
/*
Now create the delegate. this delegate will have a return void type and take ETGCarEvetArgs as argument.
* The delegate does not have to be declared inside a class. All our event handlers will have the same signature as this delegate.
But Delegate can be of any type, void/int/string/bool/or any custom type
*
* Also we can create a generic delegate [shown in third example]
*/
// Delegate 1. custom return type
public delegate Status ETGStatusEventhandler(ETGCarEvetArgs args);
// Delegate 2. custom return type
public delegate void ETGEventhandler(object sender, ETGCarEvetArgs args);
// Delegate 3. this is how we use generic to create a delegate
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
}
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;
namespace ETG.Base.Applications.ETigers.Web.ETech.CSharp
{
public class ETGCarEvetArgs : System.EventArgs
{
private int _Speed;
private string _Message;
private string _Color;
public int Speed
{
get { return _Speed; }
set { _Speed = value; }
}
public string Message
{
get { return _Message; }
set { _Message = value; }
}
public string Color
{
get { return _Color; }
set { _Color = value; }
}
}
}
Here we see the implementation.
Implementation
namespace ETG.Base.Applications.ETigers.Web.ETech.CSharp
{
public class ETGCar
{
//Create any event handler of delegate type
public event ETGEventhandler onCarStart;
public event ETGStatusEventhandler onSpeedChange;
/* Generic example:
public event EventHandler<NameEventArgs> NameReceived;
public event EventHandler<WorkflowMessageEventArgs> NameRequested;
*/
public ETGCar()
{
}
public void Start(ETGCarEvetArgs args)
{
this.onCarStart += new ETGEventhandler(ETGCar_onCarStart);
//this.onSpeedChange +=new ETGStatusEventhandler(ETGCar_onSpeedChange);
}
public void Stop(ETGCarEvetArgs args)
{
this.onCarStart += new ETGEventhandler(ETGCar_onCarStart);
// this.onSpeedChange += new ETGStatusEventhandler(ETGCar_onSpeedChange);
}
protected void ETGCar_onCarStart(object sender, ETGCarEvetArgs e)
{
string _Message = "";
switch (e.Speed)
{
case 80:
_Message = "High Speed";
break;
case 60:
_Message = "Medium Speed";
break;
case 40:
_Message = "Low Speed";
break;
}
}
protected Status ETGCar_onSpeedChange(object sender, ETGCarEvetArgs e)
{
return Status.High;
}
}
}
Privacy Policy ©2007 E-Tigers.net, All Rights Reserved Terms & Conditions
Supported by ETG Consultancy