SIGN UP MEMBER LOGIN:    
ARTICLE

Events and Delegates

Posted by Kunal Cheda Articles | C# Language July 26, 2001
Events in C# are based on delegates, the Originator defining one or more callback functions as delegates and the listening object then implements then.
Reader Level:

Events in C# are based on delegates, the Originator defining one or more callback functions. A class that wants to use events defines callback functions as delegates and the listening object then implements then.

Source Code:

EventTest.cs

using
System;
public class MyEvt
{
public delegate void t(Object sender,MyArgs e);// declare a delegate
public event t tEvt; //delcares a event for the delegate
public void mm()
{ //function that will raise the callback
MyArgs r=new MyArgs();
tEvt(
this,r); //calling the client code
}
public MyEvt(){ }

//arguments for the callback
public class MyArgs:EventArgs
{
public MyArgs(){ }
}
public class MyEvtClient
{
MyEvt oo;
public MyEvtClient()
{
this .oo=new MyEvt();
this .oo.tEvt +=new MyEvt.t(oo_tt);
}
public static void Main (String [] args)
{
MyEvtClient cc=
new MyEvtClient();
cc.oo.mm();
}
//this code will be called from the server
public void oo_tt(object sender, MyArgs e)
{
Console.WriteLine("yes");
}
}

Save the File
as EventTest.cs
Compile C:\>csc EventTest.cs
run C:\>EventTest

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor