Visual Basic.NET
  Home arrow Visual Basic.NET arrow 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 / 5
    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


    (Page 1 of 6 )

    This article gives you a solid understanding of delegates using Visual Basic.NET 2005. You will need a good foundation in object-oriented programming and Visual Basic.NET to understand this article.

    If you are new to OOP in Visual Basic.NET, I strongly suggest that you read the articles you will find at the following links:

    http://www.aspfree.com/c/a/VB.NET/Using-Constructors-with-Object-Oriented-Database-Development-with-VB-NET-2005/

    http://www.aspfree.com/c/a/VB.NET/Properties-and-Object-Oriented-Database-Development-with-VBNET-2005/

    http://www.aspfree.com/c/a/VB.NET/Using-Methods-with-Object-Oriented-Database-Development-with-VB-NET-2005/

    http://www.aspfree.com/c/a/VB.NET/Inheritance-with-VB-NET-2005/

    http://www.aspfree.com/c/a/VB.NET/Understanding-Custom-Events-using-Visual-Basic-NET-2005/

    The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. I didn't really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.

    A simple introduction to Delegates using Visual Basic 2005

    A delegate allows us to encapsulate a reference to a method inside an object -- a delegate object, to be precise. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

    Before trying to understand the above, let us work with a simple example. The following is a sample class:


    Public Class Sample01

    Private _x As Integer

    Private _y As Integer


    Public Sub New()


    End Sub


    Public Sub New(ByVal a As Integer, ByVal b As Integer)

    _x = a

    _y = b

    End Sub


    Public Property X() As Integer

    Get

    Return _x

    End Get

    Set(ByVal value As Integer)

    _x = value

    End Set

    End Property


    Public Property Y() As Integer

    Get

    Return _y

    End Get

    Set(ByVal value As Integer)

    _y = value

    End Set

    End Property


    Public Sub Add()

    MessageBox.Show("Sum = " & (Me.X + Me.Y))

    End Sub


    Public Sub Multiply()

    MessageBox.Show("Product = " & (Me.X * Me.Y))

    End Sub

    End Class



    The above class has two private fields ("_x" and "_y") which are only accessible within the class, and not outside the class. Further, it has two public properties and two public methods, "Add" and "Multiply." Note that public members are accessible even outside the class.

    To test the above class, add a new form with two buttons and a label. Modify your code to match the following:


    'without using delegates

    Public Class Form1


    Dim obj As New Sample01(10, 20)


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

    obj.Add()

    End Sub


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

    obj.Multiply()

    End Sub


    End Class

    There is nothing new in the above form. We are simply instantiating an object based on the "Sample01" class and calling its methods. We will do the same in the next section, but with "Delegates."

    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

    - 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...
    - Polymorphism using Abstract Classes in Visua...





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