SIGN UP MEMBER LOGIN:    
ARTICLE

Implementing Delegates in C#

Posted by TimothyA Vanover Articles | C# Language February 04, 2001
The Zip file contains two projects. One is VB.Net and the other is C#. Each project is an example of the same use of a delegate. A base class is derived and the delegate calls a method on several classes derived from the base class.
Reader Level:
Download Files:
 

The Zip file contains two projects. One is VB.Net and the other is C#. Each project is an example of the same use of a delegate. A base class is derived and the delegate calls a method on several classes derived from the base class. Several things are shown from this example. Using inherited base types to strinct type checking of a base type and calling class level methods from a single delegate.

Here is sample code from Delegates.cs file:

namespace DelegatesCS
{
using
System;
/// <summary>

///
Author TVanover@Quilogy.Com
///
Date 02/02/2001
///
Purpose Example of Delegate usage
/// </summary>

public class Wisdom
//class containing the Delegate
{
public delegate string
GiveAdvice();
public string
OfferAdvice(GiveAdvice Words)
{
return
Words();
}
}
public class Parent
//base class
{
public virtual string
Advice()
{
return
("Listen to reason");
}
~Parent() {}
}
public class Dad: Parent
//derive from the parent
{
public
Dad() {}
public override string
Advice()
{
return
("Listen to your Mom");
}
~Dad() {}
}
public class Mom: Parent
//derive from the parent
{
public
Mom() {}
public override string
Advice()
{
return
("Listen to your Dad");
}
~Mom() {}
}
public class Daughter
//don't derive from the parent
{
public
Daughter() {}
public string
Advice()
{
return
("I know all there is to life");
}
~Daughter() {}
}
public class
Test
{
public static string CallAdvice(Parent p)
//use the base type of derived class
{
Wisdom parents =
new
Wisdom();
Wisdom.GiveAdvice TeenageGirls =
new
Wisdom.GiveAdvice(p.Advice);
return
(parents.OfferAdvice(TeenageGirls));
}
public static void
Main()
{
Dad d =
new
Dad();
Mom m =
new
Mom();
Daughter g =
new
Daughter();
//these both derived from the base class
Console.WriteLine(CallAdvice(d));
Console.WriteLine(CallAdvice(m));
//cannot do this as it did not derive from the base
//Console.WriteLine(CallAdvice(g));
}
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor