C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




events and delegates


Posted Date: 15 May 2007      Total Responses: 9

Posted By: swathi       Member Level: Gold     Points: 2


brief about Event and Delegate?
difference between Event and delegate?
how can we call delegate at subprogram?
how can we call event at subprogram?




Responses

Author: Shivshanker Cheral    16 May 2007Member Level: DiamondRating:     Points: 2
HI
Refer this
http://www.expresscomputeronline.com/20021216/techspace2.shtml


Author: Shivshanker Cheral    16 May 2007Member Level: DiamondRating:     Points: 2
Hi swathi refer this
http://www.codeproject.com/useritems/Events_and_Delegates.asp


Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
deligate is function pointer
event use deligate


Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
A delegate is an important element of C# and is extensively used in every type of .NET application. A delegate is a class whose object (delegate object) can store a set of references to methods. This delegate object is used to invoke the methods. Many developers find delegates complicated.


Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
how to declare and use a delegate.

class sample
{
delegate void del1();
delegate void del2 (int i);
public void fun()
{
del1 d1 = new del1 (f1);
d1();
del2 d2 = new del2 (f2) ;
d2 ( 5 ) ;
}
public void f1()
{
Console.WriteLine (“Reached in f1”) ;
}
public void f2 (int i)
{
Console.WriteLine (“Reached in f2”) ;
}
}



Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
Delegates in Inheritance
Suppose there is a base class and a class derived from this class. A method is invoked from a base class method using a delegate. We can initialise this delegate in derived class so that when the method is invoked from the base class it is the derived class’s method that would get called.


Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
using System;

namespace delegateinherit
{
public delegate void del1();
class mybase
{
public del1 d;
public void fun()
{
d();
}
}
class der : mybase
{
public der()
{
d = new del1 (f1);
fun();
d = new del1 (f2);
fun();
}
public void f1()
{
Console.WriteLine (“Reached in f1”) ;
}
public void f2()
{
Console.WriteLine (“Reached in f2”) ;
}
}
class Class1
{
static void Main (string[] args)
{
der d = new der();
}
}
}



Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
Where Delegates are useful
In a general WinForm application, a class Form1 gets created, which is derived from the Form class. If we want to handle mouse click messages, we override the OnMouseDown( ) message handler in the Form1 class. When we click the mouse button, control reaches to the OnMouseDown( ) of the derived class i.e Form1 class because of the simple rule of inheritance. But if we click on a push button on the form, the OnMouseDown( ) of the Form1 class does not get called. Because Form1 class is not derived from the class that creates button. In such a situation, delegates prove useful. Calling methods using delegate require only that the signature of methods and that of the delegate should match. It does not require any inheritance chain to call the appropriate method. So, in .NET, all the event handlers are called through delegates, instead of relying on the inheritance chain.


Author: ChandraShekarThota    27 Jun 2007Member Level: DiamondRating:     Points: 2
Delegate at work
As said earlier, all the event handlers are called using delegates. We will now see a dummy program to explain how the Paint event handler must be getting called using a delegate.

using System;
namespace delegateatwork
{
public delegate void PaintHandler();
class Form
{
public PaintHandler Paint ;
public void OnPaint()
{
Paint();
}
}
class form1 : Form
{
public form1()
{
Paint += new PaintHandler (form1_Paint);
OnPaint() ;
}
public void form1_Paint()
{
Console.WriteLine (“form1_Paint”);
}
}
class Class1
{
static void Main (string[] args)
{
form1 f = new form1();
}
}
}
We have designed a class Form1 that contains a method called Form1_Paint( ). The Form1 class is derived from the Form class. In the Form class we have declared an object Paint of the delegate class PaintHandler. In Main( ) we have instantiated an object of the Form1 class. This results in its constructor being called. In the constructor of the Form1 class we have initialised the Paint object and encapsulated in it the Form1_Paint( ) method. Next we have called the OnPaint( ) method of the Form class (in WinForm application Paint event gets generated automatically), which in turn calls the Form1_Paint( ) method via Paint.



Post Reply
You must Sign In to post a response.
Next : what is form authentication?
Previous : difference between user control and web user control
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use