Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 5 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 9
    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 - Callbacks using Delegates


    (Page 5 of 6 )

    "Callback" is one of the most important features of delegates. Let us start with an example. To make things easier to understand, I added a new class as follows:


    Public Class Sample04


    Delegate Sub FactorFound(ByVal FactorValue As Integer)


    Private _x As Integer


    Public Sub New()


    End Sub


    Public Sub New(ByVal a As Integer)

    _x = a

    End Sub


    Public Property X() As Integer

    Get

    Return _x

    End Get

    Set(ByVal value As Integer)

    _x = value

    End Set

    End Property


    Public Sub FindFactors(ByVal delgFoundFactor As FactorFound)

    For i As Integer = 1 To _x

    If _x Mod i = 0 Then

    delgFoundFactor(i)

    End If

    Next

    End Sub


    End Class


    The most important method from the above class is the following:


    Public Sub FindFactors(ByVal delgFoundFactor As FactorFound)

    For i As Integer = 1 To _x

    If _x Mod i = 0 Then

    delgFoundFactor(i)

    End If

    Next

    End Sub


    The method accepts a parameter of type "delegate" which is declared at module level as follows:


    Delegate Sub FactorFound(ByVal FactorValue As Integer)


    That means the calling program can execute the "FindFactors" method by passing the address of another method. The address, which is passed to "FindFactors," can be invoked within the same "FindFactors" method.

    In simple words, the calling program executes "FindFactors" by giving permission to the "FindFactors" method to execute (internally inside "FindFactors") another method (address) passed to it.

    The following sample can be considered a caller for this demonstration:


    'implementing callback methods using delegates

    Public Class Form6


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

    Me.ListBox1.Items.Clear()

    Dim obj As New Sample04(Me.TextBox1.Text)

    obj.FindFactors(AddressOf FactorAvailable)

    End Sub


    Private Sub FactorAvailable(ByVal FactorValue As Integer)

    Me.ListBox1.Items.Add(FactorValue)

    End Sub

    End Class


    From the above, you can observe that "FactorAvailable" is passed to the "FindFactors" method. The "FactorAvailable" method gets executed with the following statement in the "FindFactors" method:


    delgFoundFactor(i)

    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". ...
     

    VISUAL BASIC.NET ARTICLES

    - 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...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT