asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Developing with . NET Delegates
By Ramesh Balaji
Rating: 3.5 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Of the many new capabilities provided by .NET through its Common Language Runtime and .NET framework classes, "Delegates" or .NET Delegates is one worth examining.

    This article covers the following aspects of Delegates.

    a. Defining Delegates.

    b. Using Delegates

    c. Understanding how Delegate functions in the CLR Environment.

    I am going to use VB.NET (BETA 2) to run through my examples.

    Defining Delegates

    Delegate is commonly defined as a Type Safe Pointer. To better understand a Type-Safe pointer, let's see a how a callback function in C++/VC++ works. The Callback function is normally implemented in the Business tier of a 3-tier architecture (of course there are exceptions to that). Once the Business Logic component implements a particular functionality, say a complex arithmetic calculation, it informs a client that the job was performed successfully. In order to inform the client, it needs an address of the function\method implemented in the client. The address of a function is just a memory address; this address does not carry any information such as number of parameters, data type of each parameter and its return value. Since the Callback function does not have any idea of the Method Signature it is going to call, in short, it is not Type Safe.

    However, Delegates provide the feature of Callback functions in a safe way. Taking the previous example, if the Business Logic finds that the function signature implemented by the Client differs in terms of number of parameters or parameter types or return values as opposed to the method signature, it is going to safely raise an error.

    Using Delegates

    We can better understand through an example.

    As a owner of a fictitious toy firm trying attract more local customers, I am going to offer special discounts of 10% on all toys to all local residents, whom I will identify by their phone numbers. If their area code is "479" (which is my area code), I am going to raise a message to the client saying "The Customer is eligible for 10% Discount"

    This is a Normal Windows Form App written in VB.NET, which is going to accept the Customer First Name, Last Name and the Phone Number, which includes the area code and will perform the validation.

    Step 1, I am going to define a Delegate called "MakeDelegate". The code below is fundamental syntax for declaring a Delegate in VB.NET

    
    	Public Delegate Sub MakeDelegate (ByVal PhoneNo As String)
    
    
    This Delegate class "MakeDelegate" is now capable of invoking a function, which can accept a Single String Parameter and will return VOID.

    Being a Public type, it can be called anywhere inside my Application.

    Step 2,

    I am going to define a Customer Class, which has the following fields and a method.

    
    Public Class Customer
        Public FirstName As String
        Public LastName As String
    
        Public Sub ValidateCustomer (ByVal objDelegate As MakeDelegate, _
                                    ByVal PhoneNo As String)
            If PhoneNo.StartsWith ("479") Then
                objDelegate.Invoke(PhoneNo)
            End If
        End Sub
    
    End Class
    
    
    The method ValidateCustomer is going to accept the PhoneNo and a Delegate Object of type "MakeDelegate" as the parameters and validate whether it is a local code or long-distance code and invoke the Delegate Object accordingly.

    The Fields FirstName and LastName will store the customers' first and last names.

    In the third step, we are going to tell the Delegate object which Function/Procedure at the form class needs to be called when "ObjDelegate.Invoke(PhoneNo)" is called.

    
            Dim objCustomer As Customer = New Customer()
    
            Dim objDelegate As MakeDelegate
            objDelegate = AddressOf NotifyClient
    
            objCustomer.FirstName = txtFirstName.Text
            objCustomer.LastName = txtLastName.Text
    
            objCustomer.ValidateCustomer(objDelegate, txtPhoneNo.Text)
    
    
    If we have a look at the above code, Customer Class instance is created and the reference variable for the "MakeDelegate" Delegate object is assigned.

    Now examine the following line

    
    objDelegate = AddressOf NotifyClient
    
    
    We assign the local procedure "NotifyClient", which is declared and defined inside the Windows Form Class to the Delegate Object.
      
     Private Sub NotifyClient(ByVal PhoneNo As String)
            MsgBox("This Customer is Eligible for 10% Discount")
    End Sub
    
    
    Once we assign the instance of the Delegate, we must provide the address of a method implementation with a matching method signature.

    If we carefully take a look at the NotifyClient, it also has a Single Parameter of data type String and it returns Void. The signature of "NotifyClient" procedure matches the signature of the Delegate "MakeDelegate".

    Assume that if MakeDelegate declaration looks like the declaration below

    
    Public Delegate Sub MakeDelegate(ByVal PhoneNo As String, byval SomeValue as long)
    
    
    then I am going to get a Compile time error. This is what we call Type Safe function pointers. If we look at the way .NET implements delegates, it always sees the matching signature starting with the function parameters, their data types, and the return values.

    In a summary, If I run this program, the Customer class will be instantiated along with the delegate object "MakeDelegate", the Address of local form class procedure NotifyClient is assigned to the Delegate class.

    The ValidateCustomer Method of the Customer Class is called and it checks the PhoneNo. When it finds the phone number starts with "479", it is going to call the Invoke method of the MakeDelegate object which will eventually call the "NotifyClient" function.

    Understanding Delegates

    The implementation of Delegates is not really complicated because CLR and VB.NET compiler does a lot of things behind the scenes.

    Let's re-examine the following code

    
    Public delegate MakeDelegate(phoneno)
    
    
    When the VB.NET compiler sees the above line, it creates a public class "MakeDelegate" because it is declared as Public Delegate. If it would have been declared as Private, then it is going to create a Private class. This class will inherit from System.MultiCastDelegate. Which means all properties and methods of System.MulticastDelegate class will be inherited to the "MakeDelegate" Delegate Class
    
    __[CLS] MakeDelegate
       |   |   |     .class public auto ansi sealed 
       |   |   |      extends [mscorlib]System.MulticastDelegate 
       |   |   |___[MET] method .ctor : void(object,native int)
       |   |   |___[MET] method BeginInvoke : class [mscorlib]System.IAsyncResult(string,class 
    [mscorlib]System.AsyncCallback,object)
       |   |   |___[MET] method EndInvoke : void(string,class [mscorlib]System.IAsyncResult)
       |   |   |___[MET] method Invoke : void(string)
       |   |
    
    
    I also pasted the ILDASM code above for further clarification.

    When you take a look at the Delegate Constructor (bolded) in the ILDASM code, there are two parameters. The first parameter indicates the reference of the object being passed and the second parameter is the int32 value that identifies the Method. From our example, it should have "objCustomer" as the value for object reference parameter and "NotifyClient" as the value in the MethodInfo parameter.

    The reference to object and method value is saved on the private fields respectively. In case you pass the Static Object, the object reference is set to NULL.

    Now that we understand how the Delegate constructs objects, let's see how the method is actually invoked.

    I've pasted the line from ILDASM, that calls the invoke method of the Delegate Class.

    
     	|___[MET] method Invoke : void(string)
    
    
    Look at it carefully. The compiler actually generates code to call the delegate object invoke method whenever it sees this line of code
    
    objDelegate.Invoke(PhoneNo)
    
    
    This is how the .NET Delegate class works internally. However, it performs lots of things internally when it needs to invoke a chain of Delegate objects and during Asynchronous calls, but they are beyond the scope of this article.

    Conclusion

    In summary we now understand:

    a. What is a Delegate and its purpose.

    b. How to implement Delegates.

    c. How a Delegate works

    What I have given is just an introduction. If you would like to delve deeper into Delegate Classes, there are lot of useful articles you can find in http://msdn.microsoft.com (search for .NET Delegates). Happy reading!

    About the Author

    Ramesh Balaji develops business applications using ASP, VB and SQL Server. He's a frequent contributor to the www.4guysfromrolla.com and www.asptoday.com. When not programming, he enjoys spending time with his kids. He can be reached at iambramesh@yahoo.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Feb 23, 2005 - My Feature in Visual Basic 2005
    In this article, Thiru Thangarathinam demonstrates the different classes and features available through the My namespace. By providing a speed-dial that allows you to more quickly and effectively utilize .NET framework functionalities in your application, the My feature provides huge productivity improvements for .NET developers.
    [Read This Article]  [Top]
    Oct 6, 2004 - Creating Triggers Using Managed Code in SQL Server 2005
    Thiru Thangarathinam discusses taking advantage of the integation between the .NET CLR and SQL Server 2005 in order to do things like create triggers using managed code.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Aug 17, 2004 - The Perfect Service - Part 2
    Ambrose Little provides the complete source code for his 'Perfect Service' and explains how the .NET Service Manager enables features such as drag-n-drop deployment.
    [Read This Article]  [Top]
    Aug 12, 2004 - Middle-Tier Hosting: Enterprise Services, IIS, DCOM, Web Services, and Remoting
    There is broad-reaching debate about remoting, Web services, Enterprise Services, and DCOM. In short, it is a debate about the best technology to use when implementing client/server communication in .NET. Rocky Lhotka shares his thoughts on the issue while offering clear explanations of basic application architecture terminology.
    [Read This Article]  [Top]
    Jul 21, 2004 - COM Interop Exposed
    This article provides and excellent foundation for COM Interop. It reviews COM's background, explains how VB6 interacts with COM, and then shows how to design .NET components to smoothly interact with COM.
    [Read This Article]  [Top]
    Jun 24, 2004 - The Perfect Service - Part 1
    The first article in this two-part series shows how to get Ambrose Little's .NET Service Manager running and then how to add plug-n-play services to it using drag-n-drop or XCOPY.
    [Read This Article]  [Top]
    May 25, 2004 - Generics In-Depth
    Although generics are extremely useful, they also seem to have a certain mystique that cannot be readily explained. This article hopes to remove that aura of mystery by showing just how easy it is to use generics and how useful they can be in many common situations.
    [Read This Article]  [Top]
    May 11, 2004 - SharePoint Security and .NET Impersonation
    When implementing custom components that require access to restricted resources, implicit impersonation must be used. Jay Nathan shows how to create a class that makes using .NET Impersonation a snap.
    [Read This Article]  [Top]
    Mar 23, 2004 - Exploiting .NET's Advanced Deployment Features
    Tony Arslan shows how to use VS .NET's custom deployment feature to create configuration files on the target machine during installation.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers