Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Understanding Delegates using Visual Basic...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Understanding Delegates using Visual Basic.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2008-10-08

    Table of Contents:
  • Understanding Delegates using Visual Basic.NET 2005
  • A simple introduction Continued
  • Delegates with parameters using Visual Basic 2005
  • Delegates to methods returning values using Visual Basic 2005
  • Callbacks using Delegates
  • Multicast Delegates

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Understanding Delegates using Visual Basic.NET 2005 - A simple introduction Continued


    (Page 2 of 6 )

    In the previous section, a class named "Sample01" was introduced. The following is a simple "delegate" way of coding:


    'sample of using delegate

    Public Class Form2


    Delegate Sub Calculate()

    Dim obj As New Sample01(10, 20)


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim delg As New Calculate(AddressOf obj.Add)

    delg.Invoke()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim delg As New Calculate(AddressOf obj.Multiply)

    delg.Invoke()

    End Sub

    End Class


    Let us try to understand it step by step. First of all we have the following:


    Delegate Sub Calculate()


    The line says that "Calculate" is a delegate. Even though its signature looks like a method, it is actually a class. Consider "Calculate" to be a named class of yours having its own functionality to invoke "methods of other objects dynamically."

    Further proceeding we have the following:


    Dim obj As New Sample01(10, 20)


    It is simply an instantiation. Further on we have the following:


    Dim delg As New Calculate(AddressOf obj.Add)


    As previously described, "Calculate" is a class. And now, "delg" is an instance of the class "Calculate," which is "authorized" to access and execute the method named "obj.Add()".

    The "obj.Add()" method gets executed when the following statement is invoked:


    delg.Invoke()


    Finally, a delegate is simply an object which can execute methods of other objects dynamically at run time.

    To make all of this simpler, the above can also be written as follows:


    'without creating instances of a delegate and without using invoke

    Public Class Form3


    Delegate Sub Calculate()

    Dim obj As New Sample01(10, 20)

    Dim deleg As Calculate


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    deleg = AddressOf obj.Add

    deleg()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    deleg = AddressOf obj.Multiply

    deleg()

    End Sub

    End Class


    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hi guys,This is my new article which goes in to my series of "OOPs in VB.NET". ...
       · Hi, Great article, I have however been tought never to pop open a messgagebox...
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek